user3024760
user3024760

Reputation: 176

GroupBy LINQ to SQL not working, perhaps

I'm struggling to get groupby working in LINQ to SQL, im pretty new to it...I trying to groupby g.id but it just wont work...any help would be greatly appreciated...Cheers

            IQueryable<GuestList> query = from t in _ttx.Trips
            join l in _ttx.Legs on t.Id equals l.TripId
            join gl in _ttx.GuestLegs on l.Id equals gl.LegId
            join g in _ttx.Guests on gl.GuestId equals g.Id
            where t.Id == id

            select new GuestList()
            {
                Id = g.Id,
                Name = g.Name,
                NoOfLegs = g.GuestLegs.Count()
            };  

My result is

1 paul 3
2 Jim  1
1 paul 3
1 paul 3

Upvotes: 1

Views: 76

Answers (1)

Sampath
Sampath

Reputation: 65978

Please try is as below.

 var query2 = (from ps in query
               group ps by new { ps.Id } into prod
               select new GuestList
                 {
                   Id = prod.Key.Id,
                   Name = prod.Name,
                   NoOfLegs = prod.Sum(c => c.NoOfLegs),
                  }).OrderBy(x => x.Id).ToList();

Upvotes: 1

Related Questions