Myo Min Lin
Myo Min Lin

Reputation: 35

LINQ group by for count

I want to get count even though count is 0 or not in linq Group BY. This is my linq group by. In foreach looping, loop out if there is no count in var g. I want to get this var g even count is 0

var x = from b in context.Bookings
                    where b.FlightID == f.FlightID
                    group b by b.FlightID into g                        
                    select new
                    {                            
                        Count = g.Count()
                    };                


                foreach (var g in x)
            {
                var c = (from a in context.Aeroplanes
                         where a.AeroplaneID == f.AeroplaneID
                         select a.Capacity).First();

                int capacity = c;

                int i = g.Count;

                if (capacity - i >= NoOfPass)
                {
                    returnList.Add(f);
                }
            }

Upvotes: 1

Views: 186

Answers (1)

user1780943
user1780943

Reputation: 21

In this case I think group by is not needed. It can simply done by select and count the rows of the result.

var x = from b in context.Bookings
        where b.FlightID == f.FlightID
select b;
int count= x.count(); 

Upvotes: 2

Related Questions