Recovery Recovery - 4 months ago 11
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.

Answer

You need to use GroupBy, OrderBy and Count combination:

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