我有一本看起來像這樣的字典:
public Dictionary<string, List<EquipmentRow>> TableRows { get; set; }
EquipmentRow 類:
public class EquipmentRow
{
public string Model { get; set; }
public bool New { get; set; }
}
我想創建/過濾一個結構與前一個字典相同的字典,它只包含屬性New等于的專案串列true。如何通過使用 Lambda 運算式來實作?例如:
var newLocationDevices = locationDevices.Where(x => x.Value.Where())等等
uj5u.com熱心網友回復:
您可以使用以下代碼簡單地做到這一點:
var newLocationDevices = locationDevices
.ToDictionary(
o => o.Key,
o => o.Value.Where(i => i.New).ToList()
);
uj5u.com熱心網友回復:
.Where()您可以使用子句過濾字典并使用檢查bool值.Any(),
var result = locationDevices
.Where(x => x.Value.Any(x => x.New)) //Filter existing dictionary
.ToDictionary(x => x.Key, x => x.Value.Where(y => y.New).ToList()); //Create new Dictionary.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/487967.html
