LINQ: get '使用new关键字创建对象实例'当返回多条记录时
...LINQ 新手,需要一些帮助。
我正在查询一些远程 xml 并使用 LINQ 将结果放入列表中。当 xml 查询仅返回 1 条记录时,一切都很好,我通过访问列表中的 [0] 项获得完全填充的“GetMPResult”对象。
...但是,如果 xml 中返回多于 1 条记录,我会得到“使用 new 关键字创建对象实例”,
有什么想法吗?
XDocument myData = XDocument.Parse(e.Result);
List<GetMPResult> mp = (from mpItem in myData.Descendants("twfy")
select new GetMPResult
{
Member_id = mpItem.Element("member_id").Value,
House = mpItem.Element("house").Value,
First_name = mpItem.Element("first_name").Value,
Last_name = mpItem.Element("last_name").Value,
Constituency = mpItem.Element("constituency").Value,
Party = mpItem.Element("party").Value,
Entered_house = mpItem.Element("entered_house").Value,
Entered_reason = mpItem.Element("entered_reason").Value,
Left_house = mpItem.Element("left_house").Value,
Left_reason = mpItem.Element("left_reason").Value,
Person_id = mpItem.Element("person_id").Value,
Title = mpItem.Element("title").Value,
Lastupdate = mpItem.Element("lastupdate").Value,
Full_name = mpItem.Element("full_name").Value,
Url = mpItem.Element("url").Value,
Image = mpItem.Element("image").Value,
Image_height = mpItem.Element("image_height").Value,
Image_width = mpItem.Element("image_width").Value,
}).ToList<GetMPResult>();
GetMPCompleteEvent(mp[0]);
...new to LINQ and could do with some help.
I'm querying some remote xml and using LINQ to put the results into a List. When the xml query only returns 1 record all is fine and i get a fully populated 'GetMPResult' object by accessing [0] item in the list.
...but, if there is more than 1 record returned in the xml, i get 'Use the new keyword to create an object instance'
any ideas?
XDocument myData = XDocument.Parse(e.Result);
List<GetMPResult> mp = (from mpItem in myData.Descendants("twfy")
select new GetMPResult
{
Member_id = mpItem.Element("member_id").Value,
House = mpItem.Element("house").Value,
First_name = mpItem.Element("first_name").Value,
Last_name = mpItem.Element("last_name").Value,
Constituency = mpItem.Element("constituency").Value,
Party = mpItem.Element("party").Value,
Entered_house = mpItem.Element("entered_house").Value,
Entered_reason = mpItem.Element("entered_reason").Value,
Left_house = mpItem.Element("left_house").Value,
Left_reason = mpItem.Element("left_reason").Value,
Person_id = mpItem.Element("person_id").Value,
Title = mpItem.Element("title").Value,
Lastupdate = mpItem.Element("lastupdate").Value,
Full_name = mpItem.Element("full_name").Value,
Url = mpItem.Element("url").Value,
Image = mpItem.Element("image").Value,
Image_height = mpItem.Element("image_height").Value,
Image_width = mpItem.Element("image_width").Value,
}).ToList<GetMPResult>();
GetMPCompleteEvent(mp[0]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来与对象的数量无关,因为它是来自
NullReferenceException
的错误消息。我认为您正在处理的元素中有一些元素并不存在,即使您期望它存在,因此您会得到空引用。
尝试注释掉初始化列表中的所有属性设置器(
select new GetMPResult
)——如果它没有抛出,则意味着您发现了问题。This doesn't look like it has to do with the number of objects, since it's the error message from
NullReferenceException
.I think that there's simply some element among those you are processing that does not exist even though you are expecting it, so you get the null reference.
Try commenting out all the property setters in the initialization list (
select new GetMPResult
) -- if it doesn't throw, it means you found the problem.