C# List<对象> 转成Dictionary<key,List<对象>> Linq 语句怎么写? 用一个cars.GroupBy(o=>Name) 不就可以得到同样的么。。如果非要Dictionary那只能Foreach了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 List<Car> cars = new List<Car>(); cars.Add(new Car() { ID = 1, Name = "红旗", OtherName = "hq1" }); cars.Add(new Car() { ID = 2, Name = "奔驰", OtherName = "bc1" }); cars.Add(new Car() { ID = 3, Name = "宝马", OtherName = "bm1" }); cars.Add(new Car() { ID = 4, Name = "奔驰", OtherName = "bc2" }); cars.Add(new Car() { ID = 5, Name = "宝马", OtherName = "bm2" }); var carDict = cars.GroupBy(x => x.Name).ToDictionary(x => x.First().Name, y => { List<Car> tempList = new List<Car>(); tempList.AddRange(y); return tempList; }); //以下是输出: foreach (KeyValuePair<string, List<Car>> pair in carDict) { Console.WriteLine(pair.Key); pair.Value.ForEach(x => Console.WriteLine(@"ID={0}, Name='{1}',OtherName='{2}'", x.ID, x.Name, x.OtherName)); Console.WriteLine(); }输出结果:红旗ID=1, Name='红旗',OtherName='hq1'奔驰ID=2, Name='奔驰',OtherName='bc1'ID=4, Name='奔驰',OtherName='bc2'宝马ID=3, Name='宝马',OtherName='bm1'ID=5, Name='宝马',OtherName='bm2' 还可以简化一点: List<Car> cars = new List<Car>(); cars.Add(new Car() { ID = 1, Name = "红旗", OtherName = "hq1" }); cars.Add(new Car() { ID = 2, Name = "奔驰", OtherName = "bc1" }); cars.Add(new Car() { ID = 3, Name = "宝马", OtherName = "bm1" }); cars.Add(new Car() { ID = 4, Name = "奔驰", OtherName = "bc2" }); cars.Add(new Car() { ID = 5, Name = "宝马", OtherName = "bm2" }); var carDict = cars.GroupBy(x => x.Name).ToDictionary(x => x.First().Name, y => new List<Car>(y)); //以下是输出: foreach (KeyValuePair<string, List<Car>> pair in carDict) { Console.WriteLine(pair.Key); pair.Value.ForEach(x => Console.WriteLine(@"ID={0}, Name='{1}',OtherName='{2}'", x.ID, x.Name, x.OtherName)); Console.WriteLine(); } var carDict = cars.GroupBy(o => o.Name).ToDictionary(a => a.First().Name, y => y.ToList());再简化 实际上核心的代码就一句,o(∩_∩)o...哈哈!!! var carDict = cars.GroupBy(x => x.Name).ToDictionary(x => x.First().Name, y => new List<Car>(y)); 对,直接ToList就可以了,一下忘了。 var carDict = cars.GroupBy(x => x.Name).ToDictionary(x => x.Key, x => x.ToList()); C# 内存操作 byte[]数组 模糊查询 定位 求思路和解决方案 如何屏蔽AdvTree节点拖拽时改变层级关系? datagirdview分页的问题 [SQL问题]关于字段默认值问题,详细如下 这个错误是什么意思啊 基础问题。。没搞懂。。 C# 线程(ref)问题 .net比起VB,VC,JAVA等语言有什么明显的特点 恶意控件拦截,与防止恶意网站注册表写入! PrintPreviewDialog 会被打印两次的原因? JAVA 写的webService,我用soapclient客户端调用,怎么传递参数及i进去啊 关于datagridview加载的问题
cars.Add(new Car() { ID = 1, Name = "红旗", OtherName = "hq1" });
cars.Add(new Car() { ID = 2, Name = "奔驰", OtherName = "bc1" });
cars.Add(new Car() { ID = 3, Name = "宝马", OtherName = "bm1" });
cars.Add(new Car() { ID = 4, Name = "奔驰", OtherName = "bc2" });
cars.Add(new Car() { ID = 5, Name = "宝马", OtherName = "bm2" });
var carDict = cars.GroupBy(x => x.Name).ToDictionary(x => x.First().Name, y =>
{
List<Car> tempList = new List<Car>();
tempList.AddRange(y);
return tempList;
}); //以下是输出:
foreach (KeyValuePair<string, List<Car>> pair in carDict)
{
Console.WriteLine(pair.Key);
pair.Value.ForEach(x => Console.WriteLine(@"ID={0}, Name='{1}',OtherName='{2}'", x.ID, x.Name, x.OtherName));
Console.WriteLine();
}
输出结果:
红旗
ID=1, Name='红旗',OtherName='hq1'奔驰
ID=2, Name='奔驰',OtherName='bc1'
ID=4, Name='奔驰',OtherName='bc2'宝马
ID=3, Name='宝马',OtherName='bm1'
ID=5, Name='宝马',OtherName='bm2'
List<Car> cars = new List<Car>();
cars.Add(new Car() { ID = 1, Name = "红旗", OtherName = "hq1" });
cars.Add(new Car() { ID = 2, Name = "奔驰", OtherName = "bc1" });
cars.Add(new Car() { ID = 3, Name = "宝马", OtherName = "bm1" });
cars.Add(new Car() { ID = 4, Name = "奔驰", OtherName = "bc2" });
cars.Add(new Car() { ID = 5, Name = "宝马", OtherName = "bm2" });
var carDict = cars.GroupBy(x => x.Name).ToDictionary(x => x.First().Name, y => new List<Car>(y)); //以下是输出:
foreach (KeyValuePair<string, List<Car>> pair in carDict)
{
Console.WriteLine(pair.Key);
pair.Value.ForEach(x => Console.WriteLine(@"ID={0}, Name='{1}',OtherName='{2}'", x.ID, x.Name, x.OtherName));
Console.WriteLine();
}
var carDict = cars.GroupBy(o => o.Name).ToDictionary(a => a.First().Name, y => y.ToList());再简化
var carDict = cars.GroupBy(x => x.Name).ToDictionary(x => x.First().Name, y => new List<Car>(y));