问一条linq 语句 有一个list<entity> 要得到 entit.id 有那些 并得到它出现的次数
类似sql语句
select id,count(*) from table group by id
用 linq 怎么实现? ------解决方案--------------------
var query = from p in db.entity
group p by p.id into g
let countname = g.Count(x => x.id)
select new
{
id = g.Key,
Count = countname
};
------解决方案--------------------
List<entity> list = new List<entity> { new entity { id = 1, name = "shen" }, new entity { id = 3, name = "bao" }, new entity { id = 1, name = "zz" } };
var v = list.GroupBy(c => c.id).Select(c => new { c.Key, count = c.Count() });
foreach (var vsv in v)
richTextBox1.Text += (vsv.Key.ToString() + ":" + vsv.count.ToString()+"\n"); ------解决方案-------------------- 使用“Linqer.exe”,可以将Sql转换成Linq ------解决方案--------------------
var query = listEntity.GroupBy(c => c.id);
foreach (var group in query)
{
Console.WriteLine(group.Count().ToString());
}