在 linq 中,我正在尝试加入数据表。现在我如何按 linq 查询分组。我到目前为止所尝试的。
var resultstatu = from table1 in dtFetch.AsEnumerable()
join table2 in dssap.AsEnumerable() on (string)table1["SAPQuotationNo"] equals (string)table2["Quationno"]
select new {
EMDNo = (int)table1["EMDNo"],
Quationno = (string)table2["Quationno"],
CustomerCode = (string)table2["CustomerCode"],
InvoiceQuantity = (string)table2["InvoiceQuantity"],
InvoiceValue = (string)table2["InvoiceValue"]
};
现在我想为上面的 linq 编写 group by。帮助我
从上面的 linq 我得到的数据如下:
EMDNO QuationNo CustomerCode InvoiceQuantity Invoicevalue
----- --------- ------------ ------------- -------------
1234 16001 H152 20.00 450
1234 16001 H152 20.00 450
我想成为上述结果:
EMDNO QuationNo CustomerCode InvoiceQuantity Invoicevalue
----- --------- ------------ -------------- -----------
1234 16001 H152 40.00 9000
我试过这样:
var resultstatu = from table1 in dtFetch.AsEnumerable()
join table2 in dssap.AsEnumerable() on (string)table1["SAPQuotationNo"] equals (string)table2["Quationno"]
group new { table1, table2 } by
new
{
Quationno = table2.Field<string>("Quationno"),
MaterialCode = table2.Field<string>("MaterialCode"),
EMDNo = table1.Field<int>("EMDNo"),
QuotationDate= table2.Field <string>("QuotationDate"),
QuotionQuantity = table2.Field<string>("QuotionQuantity"),
InvoiceQuantity = table2.Field<string>("InvoiceQuantity"),
InvoiceValue = table2.Field<string>("InvoiceValue"),
MaterialName = table2.Field<string>("MaterialName"),
}
into sasi
select new {
EMDNo = sasi.Key .EMDNo ,
Quationno= sasi.Key.Quationno ,
QuotationDate=sasi.Key.QuotationDate ,
MaterialCode= sasi.Key.MaterialCode ,
QuotionQuantity=sasi.Key.QuotionQuantity ,
MaterialName=sasi .Key.MaterialName,
InvoiceQuantity = Convert.ToString(sasi.Sum(x => x.table2.Field<decimal>("InvoiceQuantity"))),
InvoiceValue = Convert.ToString(sasi.Sum(x => x.table2.Field<decimal>("InvoiceValue")))
//InvoiceQuantity = sasi.Key.InvoiceQuantity,
//InvoiceValue = sasi.Key.InvoiceValue
};
上面的 linq 查询出现异常。指定的演员表无效。
我尝试了如下所示的 linq:
var resultstatu = from table1 in dtFetch.AsEnumerable()
join table2 in dssap.AsEnumerable() on (string)table1["SAPQuotationNo"] equals (string)table2["Quationno"]
group new { table1, table2 } by
new
{
Quationno = table2.Field<string>("Quationno"),
MaterialCode = table2.Field<string>("MaterialCode"),
EMDNo = table1.Field<int>("EMDNo"),
QuotationDate = table2.Field<string>("QuotationDate"),
QuotionQuantity = table2.Field<string>("QuotionQuantity"),
InvoiceQuantity = table2.Field<string>("InvoiceQuantity"),
InvoiceValue = table2.Field<string>("InvoiceValue"),
MaterialName = table2.Field<string>("MaterialName"),
}
into sasi
select new {
EMDNo = sasi.Key .EMDNo ,
Quationno= sasi.Key.Quationno ,
QuotationDate=sasi.Key.QuotationDate ,
MaterialCode= sasi.Key.MaterialCode ,
QuotionQuantity=sasi.Key.QuotionQuantity ,
MaterialName=sasi .Key.MaterialName,
InvoiceQuantity = sasi.Sum((r) => decimal.Parse(r.table2.Field<string>("InvoiceQuantity"))).ToString(),
InvoiceValue = sasi.Sum((r) => decimal.Parse(r.table2.Field<string>("InvoiceValue"))).ToString() }
;
但从上面的查询中,它正在考虑组中的“invoicequantity,invoicevalue”。如果“InvoiceQuanity”相同,则为总和,否则不是总和。相同的“Invoicevalue”如果 invoicevalue 与 450 相同,那么它是求和,否则它不是总和