在linq我想加入到數據表。現在我怎麼可以通過linq查詢進行分組。 我到現在爲止嘗試過。linq中的group by子句?加入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編寫組。幫我
從上面的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是相同的像450那麼它總結其他明智它不是總和
WEL l您在LINQ中進行了哪些分組研究?搜索「group by linq msdn」得到很多結果... –
yeah jon Skeet,但是我想通過比我更多的專欄我嘗試了:通過table2.Field(「Quationno」)對新的{table1,table2}進行分組。進入薩西。但我想要多個像Emdno這樣的fild,這樣的客戶代碼 –
在Stack Overflow中搜索「由多個列linq組成的組」,並且您將獲得* lots *的匹配... –