[英]Using LINQ, find the minimum value of an item property, in a collection, in a dictionary
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”在所有“dict”的集合中都為空 。
我似乎無法繞過這一個。
任何指導將不勝感激。
使用.SelectMany
將所有集合合並為一個大序列,然后只使用標准.Where
和.Min
運算符:
TheDictionary.Values
.SelectMany(x => x)
.Where(x => x.iMayBeNull == null)
.Min(x => x.findMe);
與SelectMany
方法並行的LINQ表達式是multiple from子句。
例:
var seq = from col in dict.Values
from item in col
where item.iMayBeNull == null
select item.findMe;
var min = seq.Min();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.