2012-04-13 19 views
3
public class Item 
{ 
    public double findMe{ get; set; } 
    public int? iMayBeNull { get; set; } 
} 

public Dictionary<int, ICollection<Item>> TheDictionary{ get; set; } 

... 

TheDictionary dict = new Dictionary<int, ICollection<Item>>(); 

我試圖找到「findMe」,其中「iMayBeNull」 是所有的空的最小值「字典「的集合。使用LINQ,找到一個項目屬性的最小值,集合中,在字典中

我似乎無法圍繞這一個包裹我的頭。

任何指導將不勝感激。

回答

7

使用.SelectMany到所有集合合併成一個大的序列,那麼就使用標準.Where.Min運算符:

TheDictionary.Values 
    .SelectMany(x => x) 
    .Where(x => x.iMayBeNull == null) 
    .Min(x => x.findMe); 
+0

非常感謝! SelectMany對我來說是一個新的。 – 2012-04-13 19:57:47

1

平行於SelectMany方法的LINQ表達是從子句倍數。

例子:

var seq = from col in dict.Values 
      from item in col 
      where item.iMayBeNull == null 
      select item.findMe; 

var min = seq.Min(); 
+0

謝謝Greg。也很有幫助。 – 2012-04-16 15:10:47

相關問題