[英]Sql linq query with where and group by
Iam having a serious problems with Linq. 我对Linq遇到严重问题。 I have simple sql query: 我有简单的SQL查询:
select
intMiesiac,
intRok,
Sum(intIn) as 'In',
Sum(intOut) as 'Out'
from dbo.GlobalDane
where intRampNr=1
group by intMiesiac, intRok
I query for sum of in and out for specified intRampNr and want it to be grouped by Month and Year. 我查询指定的inRampNr的总和,并希望按月和年分组。
Tried very various queries in linq but when i put result of query into datasource of gridview and then bind datasource it fails or it shows me only empty cells. 在linq中尝试了各种各样的查询,但是当我将查询结果放到gridview的数据源中然后绑定数据源时,它失败了,或者只显示了空单元格。
How I can create same statment in linq under C#? 如何在C#下的linq中创建相同的语句?
Something like the following will do it for you: 类似于以下内容的内容可以帮助您:
var results =
dataContext.GlobalDanes
.Where(gd => gd.intRampNr == 1)
.GroupBy(
gd =>
new
{
gd.intMiesiac,
gd.intRok,
})
.Select(
grp =>
new
{
grp.Key.intMiesiac,
grp.Key.intRok,
In = grp.Sum(gd => gd.intIn),
Out = grp.Sum(gd => gd.inOut),
});
Also in query syntax 同样在查询语法中
from a in db.GlobalDane
where a.intRampNr == 1
group a by new { a.intMiesiac, a.intRok, } into g
select new
{
g.Key.intMiesiac,
g.Key.intRok,
In = g.Sum(X => X.intIn),
Out = g.Sum(X => X.inOut),
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.