0

How can I write the following sql code in LINQ

select count(1) Cnt,state 
from yourtable
group by state
order by Cnt desc Limit 5

I am trying somthing like this

(from topPro in CS.state group topPro by new {
       Name = topPro.state.Name
} into g
select new{
     StateName = g.Key.Name,
     Count = g.Count(x=>x.state)
}
3
  • 1
    Stackoverflow is not a SQL-LINQ conversion tool. Show what you have tried. Commented Feb 18, 2015 at 10:17
  • How far have you got so far? Do you have LINQ working with your database at all? Is it using EF? What have you tried for your query? What went wrong? Please read tinyurl.com/stack-hints Commented Feb 18, 2015 at 10:17
  • @user2998990: use Count = g.Count() instead of Count = g.Count(x=>x.state) Commented Feb 18, 2015 at 10:27

1 Answer 1

1

I got the answer

var top5Pros = (from topPro in CS.state
group topPro by new { Name = topPro.state.Name } into g
select new
{
StateName = g.Key.Name,
Count = g.Count()
}).OrderByDescending(w => w.Count).Take(5);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.