Recovery Recovery - 3 months ago 6
SQL Question

How to use count only on column in EF

I want to use that query in ef:

select count(number) as CountOfNumber ,number, name from table_1 group by number, name order by CountOfNumber desc

How can I use count on column and add this column new name.


You need to use GroupBy, OrderBy and Count combination:

var results = (from item in db.table_1
                group item by new {, item.number } into grouping
                orderby grouping.Count() descending
                select new
                    name =,
                    number = grouping.Key.number,
                    CountOfNumber = grouping.Count()