Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 项目属性值_C#_Linq_Linq To Xml - Fatal编程技术网

C# 项目属性值

C# 项目属性值,c#,linq,linq-to-xml,C#,Linq,Linq To Xml,不确定这是否可行。我有“MarketinInventory”节点的子集: <MARKET_INVENTORY _Type="TotalSales" _MonthRangeType="Prior7To12Months" _Count="18"/> <MARKET_INVENTORY _Type="TotalSales" _MonthRangeType="Prior4To6Months" _Count="6"/> <MARKET_INVENTORY _Type="Tot

不确定这是否可行。我有“MarketinInventory”节点的子集:

<MARKET_INVENTORY _Type="TotalSales" _MonthRangeType="Prior7To12Months" _Count="18"/>
<MARKET_INVENTORY _Type="TotalSales" _MonthRangeType="Prior4To6Months" _Count="6"/>
<MARKET_INVENTORY _Type="TotalSales" _MonthRangeType="Last3Months" _Count="11"/>
<MARKET_INVENTORY _Type="TotalSales" _TrendType="Stable"/>
就我所知:

var marketInventoryTotalListings = from totalListings in xe.Descendants("MARKET_INVENTORY")
where (string) totalListings.Attribute("_Type") == "TotalSales"
select new MarketInventoryListing()
{
    Prior7To12Months = 
    (
        from thing in totalListings.Descendants()
        where (string)totalListings.Attribute("_MonthRangeType") == "Prior7To12Months"
        select thing.Attribute("_Count").Value
    )
};

如果确定每个节点只有一个节点,则可以使用FirstOrDefault,如下所示:-

var marketInventoryTotalListings = from totalListings in xe.Descendants("MARKET_INVENTORY")
  where (string)totalListings.Attribute("_Type") == "TotalSales"
  let prior712 = xe.Descendants("MARKET_INVENTORY")
        .FirstOrDefault(x => (string)x.Attribute("_MonthRangeType") == "Prior7To12Months")
  let prior46 = xe.Descendants("MARKET_INVENTORY")
        .FirstOrDefault(x => (string)x.Attribute("_MonthRangeType") == "Prior4To6Months")
  let last3 = xe.Descendants("MARKET_INVENTORY")
        .FirstOrDefault(x => (string)x.Attribute("_MonthRangeType") == "Last3Months")
  select new MarketInventoryListing
     {
         Prior7To12Months = prior712 != null ? (string)prior712.Attribute("_Count") : "",
         Prior4To6Months = prior712 != null ? (string)prior46.Attribute("_Count") : "",
         LastThreeMonths = last3 != null ? (string)last3.Attribute("_Count") : "",
     };

否则,如果它们是多个,则在MarketinInventorySisting的属性中,您应该将IEnumerable作为数据类型,而不是字符串。

它不起作用有两个原因:

Select返回IEnumerable,因为您需要一个字符串 所有的市场库存元素都在同一水平,所以来自Totalistings中的thing。后代不会返回任何东西。在这一点上,所有这些元素都是兄弟元素。 我更改了您的代码以解决这些问题,其工作原理如下:

var marketInventoryTotalListings = (from totalListings in xe.Descendants("MARKET_INVENTORY")
                                   where (string)totalListings.Attribute("_Type") == "TotalSales"
                                   select new MarketInventoryListing()
                                   {
                                      Prior7To12Months =
                                      (
                                           from thing in totalListings.Parent.Descendants()
                                           where (string)totalListings.Attribute("_MonthRangeType") == "Prior7To12Months"
                                           select thing.Attribute("_Count").Value
                                      ).FirstOrDefault(),
                                   }).FirstOrDefault();

当linq查询返回IEnumerabreahul时,prior7到12个月是string类型的-我在'let'语句中将'xdoc'更改为'xe',并在末尾添加了'FirstOrDefault',它成功了。谢谢你的回复@是的,那是个打字错误;更正。
var marketInventoryTotalListings = (from totalListings in xe.Descendants("MARKET_INVENTORY")
                                   where (string)totalListings.Attribute("_Type") == "TotalSales"
                                   select new MarketInventoryListing()
                                   {
                                      Prior7To12Months =
                                      (
                                           from thing in totalListings.Parent.Descendants()
                                           where (string)totalListings.Attribute("_MonthRangeType") == "Prior7To12Months"
                                           select thing.Attribute("_Count").Value
                                      ).FirstOrDefault(),
                                   }).FirstOrDefault();