使用linq从包含group和sum的数据表中选择更多列 [英] Select more column from datatable with group and sum using linq

查看:99
本文介绍了使用linq从包含group和sum的数据表中选择更多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在linq更新鲜

数据表包含列:

i am fresher in linq
datatable contain columns:

Idendity Name,
customer Name,
customer Contact Name,
Total Book Quantity,
Return Book Quality,
Quatan Book Qty,



group by包含三列Idendity名称,客户名称,客户联系人名称选择标记中的新列总书籍包含(总书籍定量+ Quatan书籍数量 - 退货书质量)我希望gridview中的所有列但网格包含Idendity名称,客户名称,客户联系人姓名和总帐簿列missinf列是ReturnBookQuality,QuatanBookQty请帮帮我。



我尝试过:




group by contain three column Idendity Name, customer Name, customer Contact Name new column in select stamement Total Book contain ( total book quant+Quatan Book Qty-Return Book Quality) i want all column in gridview but grid contain Idendity Name, customer Name, customer Contact Name and total book columns missinf columns are ReturnBookQuality,QuatanBookQty please help me.

What I have tried:

cmd = new SqlCommand(" ", con);

        da = new SqlDataAdapter(cmd);
        DataTable t_eight = new DataTable("t_eight");
        da.Fill(t_eight);//datatable "t_eight" fill

//linq query
    var query3 = (from a in t_eight.AsEnumerable()//t_eigth Datatable

                          group a by new
                          {//Group by with condition
    IdendityName = a.Field<string>("Idendity Name"),
    ContactPersonName = a.Field<string>("Contact Person Name"),
    CustomerName = a.Field<string>("Customer Name")
                          }
                              into g
                              select new
                              {
                                     IdendinyName = g.Key.IdendityName,
                                  ContactPersonName = g.Key.ContactPersonName,
                                  CurtomerName = g.Key.CustomerName,

    TotalBook = g.Sum(x => x.Field<int32>("Total Book Quantity")
                                      + x.Field<int32>("Quatan Book Qty") - x.Field<int32>("Return Book Quality"))
                              }
                  );

GridView1.DataSource = query3;
        GridView1.DataBind();

推荐答案

如果我理解你的话......



首先看,您必须仅按 Idendity Name 对数据进行分组,并对查询代码进行一些更改:

If i understand you well...

On the first look, you have to group data only by Idendity Name and make some changes in your query code:
var query3 = (from a in t_eight.AsEnumerable()
                      group a by new
                      {
IdendityName = a.Field<string>("Idendity Name"),
ContactPersonName = a.Field<string>("Contact Person Name"),
CustomerName = a.Field<string>("Customer Name")
                      }
                          into g
                          select new
                          {
                              IdendinyName = g.Key.IdendityName,
                              ContactPersonName = g.Key.ContactPersonName,
                              CurtomerName = g.Key.CustomerName,
                              TotalBook = g.Sum(x => x.Field<int32>("Total Book Quantity"))
                                  + g.Sum(y => y.Field<int32>("Quatan Book Qty"))
                                  - g.Sum(z => z.Field<int32>("Return Book Quality"))
                          }
              ).ToList();





如果有帮助,请告诉我......



Let me know, if it's helpful...


这篇关于使用linq从包含group和sum的数据表中选择更多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆