John Dimayuga
John Dimayuga

Reputation: 9

Convert SQL Query to LINQ ORDER BY, GROUP BY, COUNT

I'm having a hard time converting this query to LINQ. Can you guys help me.

select ChoiceID, count(ChoiceID) as aValue 
from QuestionNumber inner join 
     TestAnswer 
     on QuestionNumber.QNID = TestAnswer.QNID 
where QuestionNumber.QuestionID = 30 
group by ChoiceID 
order by aValue desc 

Thanks!

Upvotes: 0

Views: 121

Answers (1)

Gauravsa
Gauravsa

Reputation: 6524

var query = from qNo in QuestionNumber
            join tAnswer in TestAnswer on
            qNo.QNID equals tAnswer.QNID into qAnswer
            group qAnswer by qNo.ChoiceId
            where qNo.QuestionId == 30
            select new { ChoiceId = qNo.ChoiceId, 
                         QCount = qAnswer.Count() }
            orderby qAsnwer.QCount descending

Upvotes: 1

Related Questions