2015-10-13 85 views
0

不確定這是否可能。我有一個'MarketInventory'節點的子集:項目屬性值

<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"/> 

在_Type =「TotalSales」節點上過濾。

我不知道是否有可能投影_COUNT值屬性引入到這個類:

public class MarketInventoryListing 
{ 
    public string Prior7To12Months { get; set; } 
    public string Prior4To6Months { get; set; } 
    public string LastThreeMonths { get; set; } 
} 

這是據我得到:

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 
    ) 
}; 
+0

''Prior7To12Months''的類型是string''的''而LINQ查詢返回''IEnumerable的'' –

回答

0

它不工作原因有二:

  1. 選擇將返回IEnumerable的地方,因爲你需要一個字符串
  2. 所有MARKET_INVENTORY元素都在同一水平,使「在totalListings.Descendants從事物()」沒有返回任何東西。所有這些元素在這一點上都是兄弟姐妹。

我改變你的代碼,以解決這些問題,它如下工作:

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(); 
0

如果你確信不會存在每個節點只有一個節點,那麼你可以使用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") : "", 
    }; 

O無論如何,如果他們是多個,那麼你應該有IEnumerable<string>作爲MarketInventoryListing屬性中的數據類型而不是string

+0

Rahul - 我在'let'語句中將'xdoc'更改爲'xe',並將FirstOrDefault()添加到最後並且它工作正常。謝謝您的回覆! – FoolongC

+0

@ FoolongCv-是這是一個錯字;)更正。 –