Recovery Recovery - 1 year ago 78
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 Source

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()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download