不确定这是否可行。我有一个'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 :(得分: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") : "",
};
否则,如果它们是多个,那么您应该在MarketInventoryListing属性中使用IEnumerable<string>
作为数据类型,而不是string
。
答案 1 :(得分:0)
由于两个原因,它无法正常工作:
我更改了您的代码以解决这些问题,其工作原理如下:
__setattr__