【发布时间】:2017-03-22 06:25:54
【问题描述】:
我有一个对象列表列表,每个对象都包含一个字符串和一个浮点值。
我需要按字符串值(名称)对这些元素进行分组,并按浮点值的总和对这些组进行排序。
public class Element
{
public string Name;
public float Value;
public Element(string name,float value) {
Name = name;
Value = value;
}
}
List<List<Element>> elementslist = new List<List<Element>>();
elementslist.Add(new List<Element>() { new Element("Apple", 1.2f), new Element("Banana", 0) });
elementslist.Add(new List<Element>() { new Element("Apple", 2.1f), new Element("Banana", 1.4f) });
elementslist.Add(new List<Element>() { new Element("Apple", 0), new Element("Banana", 0) });
ps:有没有更智能的聚合算法来获得这个结果?也许也可以考虑“关闭”这些值按列表顺序...
非常感谢
【问题讨论】:
标签: c# linq list sum aggregation