【发布时间】:2013-05-18 18:58:28
【问题描述】:
如果我有一个类型的类
class Info
{
public Info(string first, string last) { this.First = first; this.Last = last; }
string First { get; private set; }
string Last { get; private set; }
}
还有一个列表例如:
var list = new List<Info>();
list.Add(new Info("jon", "doe");
list.Add(new Info("jane", "doe");
list.Add(new Info("bason", "borne");
list.Add(new Info("billy", "nomates");
我想映射到一个排序的姓氏列表,其中包含一个名字列表,即Dictionary<string,List<string>>,其中键是姓氏属性,列表是名字属性列表。
在上面的例子中,我想得到{ "doe" => { "jon", "jane" }, "borne" => { "jason" }, "nomates" => { "billy" } }。
是否可以使用 Linq 巧妙地做到这一点,如果可以,我将如何处理?
【问题讨论】:
-
您提到要对其进行排序,但是您的示例未排序。字典是一个无序的集合。
-
很好看 - 我不需要任何排序,只需要分组
标签: c# linq linq-to-objects