0
votes

Below is my linq query :

var objList = from p in db.Table1
              join q in db.Table2 on p.saleCategoryId equals q.Id
              join r in db.Table3 on p.landDetailId equals r.Id
              select new
              {
               p.Id,
               r.KhataNo,
               r.KhasraNo,
               q.type,
               p.saleCategoryId,
               p.area,
               p.stock
              };

From above query I have a result like below :

Id  KhataNo  KhasraNo  Type SaleCategoryId  Area  Stock
1     1AB     11AB      LOM     1            100   50
2     1AB     11AB      LOR     2            200  100
3     1CD     11CD      LOM     1            300  150
4     1CD     11CD      LOR     2            250   50

But I want to group by and summing up few columns above in following way (desired output) :

Id   KhataNo   KhasraNo  Area  Stock
1     1AB       11AB      300   150
2     1CD       11CD      550   200

Anyone can help me please to get the desired output.

1
@learningNew this is sum of stock value in a groupShilpa Soni

1 Answers

5
votes
      var objList = from p in db.Table1
          join q in db.Table2 on p.saleCategoryId equals q.Id
          join r in db.Table3 on p.landDetailId equals r.Id
          group t by new{t.KhataNo, t.KhasraNo}
          into grp
          select new
          {

           grp.key.KhataNo,
           grp.key.KhasraNo,
           area = grp.Sum(t=> t.Table1.area),
           stock = grp.Sum(t=>t.Table1.stock)
          };