linq 对List操作问题,大神请进...
public class Model
{
public string Name { get; set; }
public int Count { get; set; }
}
List<Model> list = new List<Model>();
for (int i = 0; i < 10; i++)
{
Model m = new Model();
m.Name = i + "Name";
m.Count = (i + 1);
list.Add(m);
}
linq 如何查找list 中已经存在的name 并更新其count值?
并最后统计出name值相同的所有count的总和?
------解决方案--------------------var query=from p in list
group p by p.Name into g
select new
{
Name=g.Key,
Count=g.Count()
}
------解决方案--------------------你要按Name统计Count吗?
var res=from q in list
group q by q.Name into g
select new Model {Name=g.Key, Count= g.Sum(x=>x.Count)};
------解决方案--------------------var res=from q in list group q by q.Name into g select new Model {Name=g.Key, Count= g.Sum(x=>x.Count)};
List<Model> result=res..ToList();
就这样