0

在 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 相同,那么它是求和,否则它不是总和

4

1 回答 1

0

很抱歉,我无法将此作为评论发布,但我缺乏这样做的 50 名声望。您将字段“InvoiceQuantity”作为组语句中的字符串和选择中的小数寻址。

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


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

您必须先将字符串转换为小数,然后才能使用 sum 函数:

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

如果它已经是十进制字段,只需将其作为十进制存储在 group 语句中:

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()
                        };
于 2013-09-26T15:37:04.320 回答