使用linq过滤结果 [英] Filter result using linq

查看:76
本文介绍了使用linq过滤结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个linq查询有什么问题,它显示错误? 对象引用未设置为对象的实例

What is wrong in this linq query, it is showing error? "Object reference not set to an instance of an object."

String  StoreNumber="1259";

XElement xel = xDoc.Element("Items")
                           .Elements("Item")
                           .Where(x => x.Elements("StoreNumber").SingleOrDefault().Value == StoreNumber.Trim())
                           .SingleOrDefault();



XDocumnet是


XDocumnet is

<Items>
   <Item>
    <ItemID>426</ItemID>
    <Name>1064</Name>
    <ParentID>307</ParentID>
    <DisplayName><![CDATA[1064]]></DisplayName>
    <ChildViewLayout>Store</ChildViewLayout>
    <Keywords><![CDATA[1064;]]></Keywords>
    <StoreNumber>1064</StoreNumber>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Entrance.txt;1000-Right.txt;1064-R.txt</PathFile>
  </Item>
  <Item>
    <ItemID>427</ItemID>
    <Name>1066</Name>
    <ParentID>307</ParentID>
    <DisplayName><![CDATA[1066]]></DisplayName>
    <ChildViewLayout>Store</ChildViewLayout>
    <Keywords><![CDATA[1066;]]></Keywords>
    <StoreNumber>1066</StoreNumber>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Entrance.txt;1000-Right.txt;1066-R.txt</PathFile>
  </Item>
  <Item>
    <ItemID>428</ItemID>
    <Name>1259</Name>
    <ParentID>309</ParentID>
    <DisplayName><![CDATA[1259]]></DisplayName>
    <ChildViewLayout>Store</ChildViewLayout>
    <Keywords><![CDATA[1259;]]></Keywords>
    <StoreNumber>1259</StoreNumber>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Entrance.txt;1101-Right.txt;1259-L.txt</PathFile>
  </Item>
  <Item>
    <ItemID>429</ItemID>
    <Name>1271</Name>
    <ParentID>309</ParentID>
    <DisplayName><![CDATA[1271]]></DisplayName>
    <ChildViewLayout>Store</ChildViewLayout>
    <Keywords><![CDATA[1271;]]></Keywords>
    <StoreNumber>1271</StoreNumber>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Entrance.txt;1101-Right.txt;1271-L.txt</PathFile>
  </Item>
  <Item>
    <ItemID>430</ItemID>
    <Name>Fire-Exit</Name>
    <ParentID>4</ParentID>
    <DisplayName><![CDATA[Fire Exit]]></DisplayName>
    <ItemImage>utils-fireexit.png</ItemImage>
    <ChildViewLayout>Utilities</ChildViewLayout>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Fire-Exit.txt</PathFile>
  </Item>
  <Item>
    <ItemID>431</ItemID>
    <Name>Rest-Room</Name>
    <ParentID>4</ParentID>
    <DisplayName><![CDATA[Rest Room]]></DisplayName>
    <ItemImage>utils-restroom.png</ItemImage>
    <ChildViewLayout>Utilities</ChildViewLayout>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Rest-Room.txt</PathFile>
  </Item>
  <Item>
    <ItemID>432</ItemID>
    <Name>Disabled-Rest-Room</Name>
    <ParentID>4</ParentID>
    <DisplayName><![CDATA[Disabled Rest Room]]></DisplayName>
    <ItemImage>utils-handicap.png</ItemImage>
    <ChildViewLayout>Utilities</ChildViewLayout>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Handicap-Rest-Room.txt</PathFile>
  </Item>
  <Item>
    <ItemID>433</ItemID>
    <Name>Management Office</Name>
    <ParentID>4</ParentID>
    <DisplayName><![CDATA[Management Office]]></DisplayName>
    <ChildViewLayout>Utilities</ChildViewLayout>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Management-Office.txt</PathFile>
  </Item>
  <Item>
    <ItemID>434</ItemID>
    <Name>Conference Hall</Name>
    <ParentID>4</ParentID>
    <DisplayName><![CDATA[Conference Hall]]></DisplayName>
    <ChildViewLayout>Utilities</ChildViewLayout>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Conference-Hall.txt</PathFile>
  </Item>
  <Item>
    <ItemID>435</ItemID>
    <Name>Offers</Name>
    <ParentID>0</ParentID>
    <DisplayName><![CDATA[Offers]]></DisplayName>
    <ItemImage>Offers.png</ItemImage>
    <ChildViewLayout>Button</ChildViewLayout>
  </Item>
  <Item>
    <ItemID>436</ItemID>
    <Name>For members</Name>
    <ParentID>435</ParentID>
    <DisplayName><![CDATA[For members]]></DisplayName>
    <ChildViewLayout>Offers</ChildViewLayout>
    <Tag>PromotionDataMembers</Tag>
  </Item>
  <Item>
    <ItemID>437</ItemID>
    <Name>For non members</Name>
    <ParentID>435</ParentID>
    <DisplayName><![CDATA[For non members]]></DisplayName>
    <ChildViewLayout>Offers</ChildViewLayout>
    <Tag>PromotionDataNonMembers</Tag>
  </Item>
</Items>

推荐答案

The problem is that not all your elements has StoreNumber node!
The problem is that not all your elements has StoreNumber node!


This works for me with your xml content. Sry it’s in VB.NET



This works for me with your xml content. Sry it's in VB.NET

Dim stringContent = "<items><item><itemid>426</itemid><name>1064</name><parentid>307</parentid><displayname><![CDATA[1064</displayname><childviewlayout>Store</childviewlayout><keywords><![CDATA[1064;</keywords><storenumber>1064</storenumber><storefloor>AAHOA</storefloor><pathfile>Entrance.txt;1000-Right.txt;1064-R.txt</pathfile></item><item><itemid>427</itemid><name>1066</name><parentid>307</parentid><displayname><![CDATA[1066</displayname><childviewlayout>Store</childviewlayout><keywords><![CDATA[1066;</keywords><storenumber>1066</storenumber><storefloor>AAHOA</storefloor><pathfile>Entrance.txt;1000-Right.txt;1066-R.txt</pathfile></item><item><itemid>428</itemid><name>1259</name><parentid>309</parentid><displayname><![CDATA[1259</displayname><childviewlayout>Store</childviewlayout><keywords><![CDATA[1259;</keywords><storenumber>1259</storenumber><storefloor>AAHOA</storefloor><pathfile>Entrance.txt;1101-Right.txt;1259-L.txt</pathfile></item><item><itemid>429</itemid><name>1271</name><parentid>309</parentid><displayname><![CDATA[1271</displayname><childviewlayout>Store</childviewlayout><keywords><![CDATA[1271;</keywords><storenumber>1271</storenumber><storefloor>AAHOA</storefloor><pathfile>Entrance.txt;1101-Right.txt;1271-L.txt</pathfile></item><item><itemid>430</itemid><name>Fire-Exit</name><parentid>4</parentid><displayname><![CDATA[FireExit</displayname><itemimage>utils-fireexit.png</itemimage><childviewlayout>Utilities</childviewlayout><storefloor>AAHOA</storefloor><pathfile>Fire-Exit.txt</pathfile></item><item><itemid>431</itemid><name>Rest-Room</name><parentid>4</parentid><displayname><![CDATA[RestRoom</displayname><itemimage>utils-restroom.png</itemimage><childviewlayout>Utilities</childviewlayout><storefloor>AAHOA</storefloor><pathfile>Rest-Room.txt</pathfile></item><item><itemid>432</itemid><name>Disabled-Rest-Room</name><parentid>4</parentid><displayname><![CDATA[DisabledRestRoom</displayname><itemimage>utils-handicap.png</itemimage><childviewlayout>Utilities</childviewlayout><storefloor>AAHOA</storefloor><pathfile>Handicap-Rest-Room.txt</pathfile></item><item><itemid>433</itemid><name>ManagementOffice</name><parentid>4</parentid><displayname><![CDATA[ManagementOffice</displayname><childviewlayout>Utilities</childviewlayout><storefloor>AAHOA</storefloor><pathfile>Management-Office.txt</pathfile></item><item><itemid>434</itemid><name>ConferenceHall</name><parentid>4</parentid><displayname><![CDATA[ConferenceHall</displayname><childviewlayout>Utilities</childviewlayout><storefloor>AAHOA</storefloor><pathfile>Conference-Hall.txt</pathfile></item><item><itemid>435</itemid><name>Offers</name><parentid>0</parentid><displayname><![CDATA[Offers</displayname><itemimage>Offers.png</itemimage><childviewlayout>Button</childviewlayout></item><item><itemid>436</itemid><name>Formembers</name><parentid>435</parentid><displayname><![CDATA[Formembers</displayname><childviewlayout>Offers</childviewlayout><tag>PromotionDataMembers</tag></item><item><itemid>437</itemid><name>Fornonmembers</name><parentid>435</parentid><displayname><![CDATA[Fornonmembers</displayname><childviewlayout>Offers</childviewlayout><tag>PromotionDataNonMembers</tag></item></items>"
Dim storeNumber = "1259"

Dim xDoc = XDocument.Parse(stringContent)

Dim someItems = From x in xDoc.Descendants("Item") Where x.Element("StoreNumber") = storeNumber
For Each item in someItems
    Console.WriteLine(item.ToString)
Next

Dim singleItem = xDoc.Descendants("Item").Single(Function(x) x.Element("StoreNumber") = storeNumber)
Console.WriteLine(singleItem.ToString)


Hi
I m sure the problem is SingleOrDefault()! use FirstOrDefault() instead.



WHen using SingleOrDefault, you have to be 100% sure that each element is unique, if not an exception occure. in the other hand FirstOrDefault() returns the first element if there is duplicates.



Hope it helps
Hi I m sure the problem is SingleOrDefault()! use FirstOrDefault() instead.

WHen using SingleOrDefault, you have to be 100% sure that each element is unique, if not an exception occure. in the other hand FirstOrDefault() returns the first element if there is duplicates.

Hope it helps


这篇关于使用linq过滤结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆