本文介绍了Linq to Entities简单组查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写(简单的)LINQ to Entities查询以按属性将元素分组并对其进行计数?

How to write (simple) LINQ to Entities query that groups elements by some attribut and count them?

SELECT answernumber, count(answerID) FROM answers
WHERE questionID = id
GROUB BY answernumber
ORDERBY answernumber;

那应该很简单,但是我不知道怎么写.

That should be simple but i don't know how to write it.

推荐答案

var query = answers
   .GroupBy(a => a.answernumber, a => a, (k, g) => new {answernumber = k, Count = g.Count()})
   .OrderyBy(i => i.answernumber);

反之亦然:

var query2 = from a in answers
         group a by a. answernumber into g
         orderby g.Key
         select new { answernumber = g.Key, Count = g.Count() };

这篇关于Linq to Entities简单组查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 16:07