SQL 查询到 LinqToSql
SQL query to LinqToSql
我有这个 SQL 查询,我想将其转换为 Linq-to-SQL:
现在是 Linq-to-SQL 代码的开头,但我一直在研究如何对字段进行分组并获得 SUM:
private void GetDatas()
{
DateTime d = DateTime.Now;
using (DataClasses1DataContext dc = new DataClasses1DataContext())
{
var query = from ent in dc.STK_ABC_ENT
join det in dc.STK_ABC_DET on ent.ENT_ID equals det.ENT_ID
join art in dc.FICHES_ARTICLES on ent.ART_CODE equals art.ART_CODE
where !ent.ENT_NUM_PAL.Contains("FDR_")
&& ent.ENT_OUTDATE == null
&& ent.ENT_PICKING == null
&& ent.ENT_DATE_ENT != d
// How to group here ?
// How to get SUM ??
}
}
您可以使用 group x by ColumnName into z
对列进行分组。
当您想要对多列进行分组时,您可以使用 group x by new { x.Column1, x.Column2 } into z
.
当您想要对多个表中的多个列进行分组时,您可以使用 group new { x, y } by new { x.Column, y.Column } into z
.
使用Sum
,只需在select中用lamda表达式调用即可。
示例:
var query = from ent in dc.STK_ABC_ENT
join det in dc.STK_ABC_DET on ent.ENT_ID equals det.ENT_ID
join art in dc.FICHES_ARTICLES on ent.ART_CODE equals art.ART_CODE
where !ent.ENT_NUM_PAL.Contains("FDR_") && ent.ENT_OUTDATE == null
&& ent.ENT_PICKING == null && ent.ENT_DATE_ENT != d
group new { art, ent } by new {
art.ART_CODE,
...,
ent.ENT_DATE_ENT,
...
} into grouped
select new {
ArtCode = grouped.Key.ART_CODE,
SumPdsNet = grouped.Sum(x => x.DET_PNET),
...
}
希望对你有用。
我有这个 SQL 查询,我想将其转换为 Linq-to-SQL:
现在是 Linq-to-SQL 代码的开头,但我一直在研究如何对字段进行分组并获得 SUM:
private void GetDatas()
{
DateTime d = DateTime.Now;
using (DataClasses1DataContext dc = new DataClasses1DataContext())
{
var query = from ent in dc.STK_ABC_ENT
join det in dc.STK_ABC_DET on ent.ENT_ID equals det.ENT_ID
join art in dc.FICHES_ARTICLES on ent.ART_CODE equals art.ART_CODE
where !ent.ENT_NUM_PAL.Contains("FDR_")
&& ent.ENT_OUTDATE == null
&& ent.ENT_PICKING == null
&& ent.ENT_DATE_ENT != d
// How to group here ?
// How to get SUM ??
}
}
您可以使用 group x by ColumnName into z
对列进行分组。
当您想要对多列进行分组时,您可以使用 group x by new { x.Column1, x.Column2 } into z
.
当您想要对多个表中的多个列进行分组时,您可以使用 group new { x, y } by new { x.Column, y.Column } into z
.
使用Sum
,只需在select中用lamda表达式调用即可。
示例:
var query = from ent in dc.STK_ABC_ENT
join det in dc.STK_ABC_DET on ent.ENT_ID equals det.ENT_ID
join art in dc.FICHES_ARTICLES on ent.ART_CODE equals art.ART_CODE
where !ent.ENT_NUM_PAL.Contains("FDR_") && ent.ENT_OUTDATE == null
&& ent.ENT_PICKING == null && ent.ENT_DATE_ENT != d
group new { art, ent } by new {
art.ART_CODE,
...,
ent.ENT_DATE_ENT,
...
} into grouped
select new {
ArtCode = grouped.Key.ART_CODE,
SumPdsNet = grouped.Sum(x => x.DET_PNET),
...
}
希望对你有用。