在LINQ中转换SQl Group [英] Convert SQl Group by in LINQ
本文介绍了在LINQ中转换SQl Group的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在LINQ中转换以下SQL
选择 FsEfId,Inc = Sum(FsIncA),Gr = sum(FsGrA),
IncCnt = sum( case when FsIncA> 0 然后 1 else 0 end )
来自 FormSub 其中 FsShId = 519 和 FsPrvExmS = ' Y'
group by FsEfId
解决方案
选择 FsEfId,Inc = S嗯(FsIncA),Gr = sum(FsGrA),
IncCnt = sum( case 何时 FsIncA> 0 1 else 0 end )
来自 FormSub 其中 FsShId = 519 和 FsPrvExmS = ' Y'
group by FsEfId
to将其转换为LinQ
var data = 来自 Frm FormSub
其中 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 )};
这是一个很好的参考 101 LINQ样本 [ ^ ]
这可能适用于你
var answer =(来自f中的context.FormSub
其中f.FsShId == 519 && f.FsPrvExmS =='Y'
选择新的
{
Id = f.FsEfId,
Inc = Sum(f.FsIncA),
Gr = sum(f.FsGrA) ,
IncCnt = sum(f.FsIncA> 0然后1其他0结束的情况)
})。GroupBy(a => a.fsEfId);
I want to Convert following SQL in LINQ
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
解决方案
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
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[^]
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);
这篇关于在LINQ中转换SQl Group的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文