Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to Convert following SQL in LINQ

SQL
select FsEfId, Inc = Sum(FsIncA), Gr = sum(FsGrA),
            IncCnt = sum (case when FsIncA > 0 then 1 else 0 end)
        from FormSub where FsShId = 519 and FsPrvExmS = 'Y'
        group by FsEfId
Posted

SQL
select FsEfId, Inc = Sum(FsIncA), Gr = sum(FsGrA),
            IncCnt = sum (case when FsIncA > 0 then 1 else 0 end)
        from FormSub where FsShId = 519 and FsPrvExmS = 'Y'
        group by FsEfId


to convert this into LinQ

C#
var data = from Frm in FormSub
           where Frm.FsShid == 519 && Frm.FsPrvExms == "Y"
           group Frm by Frm.fsEfld into g
           select new { ID = g.FsEfId, Inc = g.Sum(p=> p.FsIncA),
                        Gr = g.Sum(p=> p.FsGra),
                        IncCnt = g.Count(p=> p.FsIncA >= 1) };


This is a good reference 101 LINQ Samples[^]
 
Share this answer
 
Comments
priyankavm 24-Aug-13 1:30am    
Thank u Simon
But ID = g.FsEfId Giving Error
Simon_Whale 24-Aug-13 2:56am    
What is the error that its giving?
This may work out for u


var answer=(from f in context.FormSub
where f.FsShId == 519 && f.FsPrvExmS =='Y'
select new
{
Id=f.FsEfId,
Inc=Sum(f.FsIncA),
Gr=sum(f.FsGrA),
IncCnt=sum(case when f.FsIncA > 0 then 1 else 0 end)
}).GroupBy(a=>a.fsEfId);
 
Share this answer
 
v4
Comments
Simon_Whale 23-Aug-13 6:42am    
there is no case statement in LinQ

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900