![](/img/trans.png)
[英]get xml attribute value of element based on another attribute using linq
[英]How to get another specified attribute using grouping by from LINQ to XML
我正在使用LINQ to XML。
這是我遇到的問題:
var objectives = (from c in xdoc.Descendents("Condition")
group c by (int)c.Attribute("ObjectiveID") into k
select k).ToDictionary(e=> e.Key, // HERE I MUST PUT ANOTHER ATTRIBUTE FROM c)
在我發表評論的地方,我需要獲取另一個屬性(c.Attribute(“ Objective”))。 但是我無法訪問c屬性並獲得該屬性。
分組可能每個組包含一個以上的項目。 另一方面,字典的每個鍵只能包含一個值。 您可能想要ToLookup()
,它允許每個鍵的值枚舉:
var objectives = (from c in xdoc.Descendents("Condition")
group c by (int)c.Attribute("ObjectiveID") into k
select k).ToLookup(e=> e.Key, e => e.Select( x=> x.Objective));
如果您確實想使用字典,則只需選擇每個分組的第一項即可:
var objectives = (from c in xdoc.Descendents("Condition")
group c by (int)c.Attribute("ObjectiveID") into k
select k).ToDictionary(e=> e.Key, e => e.First().Objective));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.