Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 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:

C#
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();
Posted
Updated 14-May-17 20:14pm
v3
Comments
Richard Deeming 12-May-17 8:22am    
So you've bound your grid to a query that returns four columns, and you're surprised that it's only displaying the four columns returned by your query?

If you want to display all of the columns from the DataTable, then just bind your grid to that DataTable.
Maciej Los 12-May-17 9:31am    
Not clear! Please, be more specific and provide more details. Show us input data and expected result. This should help us to understand what you want to achieve.

1 solution

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...
 
Share this answer
 
v2
Comments
Member 13196273 17-May-17 2:49am    
not working
Maciej Los 17-May-17 7:01am    
"Not working" - is not informative at all!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900