【发布时间】:2016-12-08 23:03:34
【问题描述】:
我正在尝试过滤然后以元组为键对字典求和。 SO上有几个links,但没有什么广泛的。有什么建议吗?
Dictionary<Tuple<int, string, string, int, string>, double> dctMC = new Dictionary<Tuple<int, string, string, int, string>, double>();
List<int> listIDs = new List<int> { 1, 3, 5 };
// ID, TYPE, NAME, ORDER, COLOUR, WEIGHT
dctMC.Add(Tuple.Create(1, "Fruit", "Apple", 9, "Green"), 0.45);
dctMC.Add(Tuple.Create(2, "Fruit", "Grape", 5, "Red"), 0.78);
dctMC.Add(Tuple.Create(3, "Fruit", "Apple", 9, "Green"), 0.33);
dctMC.Add(Tuple.Create(4, "Fruit", "Peach", 2, "Yellow"), 0.89);
dctMC.Add(Tuple.Create(5, "Fruit", "Apple", 14, "Red"), 0.23);
// Sum dictionary where IDs in listIDs, Type = FRUIT, Name = APPLE, Order = 9, Colour = "Green"
double TOTAL = dctMC.Where(Z => listIDs.Contains(Z.Key(item1)) &&
Z.Key(item2) == "Apple" &&
Z.Key(item3) == 9 &&
Z.Key(item4) == "Green")
.Sum()
【问题讨论】:
-
非常适合添加带有值的初始化代码 - 让您更容易理解问题并帮助您解决问题 :)
标签: c# dictionary filter tuples