使用 linq 查询类对象
我正在尝试查询一个类对象。
我的类:
public class Result
{
public List<Driver> Drivers { get; set; }
public List<Vehicle> Vehicles { get; set; }
}
我有一个方法,我将此类的对象传递给该方法
public string BuildRequestXML(Result input)
{
var driverNames = new List<Name>();
input.Drivers.ForEach(cd => driverNames.Add(cd.Name));
}
,我得到对象引用未设置为实例错误@上述函数中的第二行代码。
提前致谢。 BB。
I am trying to query a class object.
My class :
public class Result
{
public List<Driver> Drivers { get; set; }
public List<Vehicle> Vehicles { get; set; }
}
I have method to which I am passing an object of this class
public string BuildRequestXML(Result input)
{
var driverNames = new List<Name>();
input.Drivers.ForEach(cd => driverNames.Add(cd.Name));
}
I get Object reference not set to instance error @ the 2nd line of code in the above function.
Thanks in advance.
BB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须调试才能找到它是哪一个,但该异常可能是因为
input
或input.Drivers
为 null。您甚至可以在Drivers
列表中包含空的Driver
。至于您的
driversName
列表,您可以将其重写为You'll have to debug to find which one it is, but that exception could be because either
input
orinput.Drivers
is null. You could even have a nullDriver
in theDrivers
list.As for your
driversName
list, you could rewrite that as我不太清楚为什么你会遇到这个问题,但是,更好的方法是使用选择“投影”,如下所示:
I am not exactly sure why you are getting this problem, however, a better approach would be to use a Select 'projection' as follows: