我试图过滤,然后用一个元组作为键求和字典。 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()
适合添加初始化代码与值 - 使它更容易理解问题,并帮助你解决它:) –