在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查询是trowing异常。指定演员表无效。
我尝试了如下所示的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”是相同的那么它是另外的总和它不是总和。相同的“发票价值”,如果发票价值与450相同,那么它就是其他明智的总和而不是总和
答案 0 :(得分:0)
对不起,我不能将此作为评论发布,但我缺乏这样做的50个声誉。 您将“InvoiceQuantity”字段作为组语句中的字符串和select中的小数字来处理。
InvoiceQuantity = table2.Field<string>("InvoiceQuantity"),
InvoiceQuantity = Convert.ToString(sasi.Sum(x => x.table2.Field<decimal>("InvoiceQuantity"))),
在使用sum-function之前,必须将字符串转换为小数:
Convert.ToString(sasi.Sum(x => Decimal.Parse(x.table2.Field<String>("InvoiceQuantity")))),
如果它已经是十进制字段,只需将其作为十进制存储在组语句中:
InvoiceQuantity = table2.Field<decimal>("InvoiceQuantity"),
编辑:
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"),
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()
};