2013-09-26 61 views
0

在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那麼它總結其他明智它不是總和

+0

WEL l您在LINQ中進行了哪些分組研究?搜索「group by linq msdn」得到很多結果... –

+0

yeah jon Skeet,但是我想通過比我更多的專欄我嘗試了:通過table2.Field (「Quationno」)對新的{table1,table2}進行分組。進入薩西。但我想要多個像Emdno這樣的fild,這樣的客戶代碼 –

+1

在Stack Overflow中搜索「由多個列linq組成的組」,並且您將獲得* lots *的匹配... –

回答

0

對不起,不能發佈此評論,但我缺乏50聲望這樣做。 您在組語句中將字段「InvoiceQuantity」作爲字符串進行尋址,並在select中將其作爲小數。

InvoiceQuantity = table2.Field<string>("InvoiceQuantity"), 


InvoiceQuantity = Convert.ToString(sasi.Sum(x => x.table2.Field<decimal>("InvoiceQuantity"))), 

您必須將字符串轉換爲十進制,然後才能使用和功能:

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() 
         }; 
+0

先生abeldenibus,我添加了我的lates查詢請檢查一次 –

+0

@Suryasasidhar:嘗試從組聲明中刪除InvoiceQuantity和InvoiceValue。看起來它不是一個你想要分組的列,但是不管價值是什麼,你都要總結它。只把這些列放在你真正想分組的語句中。如果您有不同的InvoiceQuantity/InvoiceValue並將其放入組聲明中,則會爲這些列中的每個值獲得新的結果行,並且總和僅爲此特定值的總和。 – abeldenibus

+0

非常感謝你,adeldenibus它工作正常..我會再次測試其他測試用例,然後我會標記你的答案,再次請你回覆。 –