LINQ小组由计数

时间:2014-07-19 08:24:30

标签: linq

即使在linq Group BY中计数为0,我也想得到计数。这是我的linq小组。在foreach循环中,如果var g中没有计数则循环。我想得到这个var g even count是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);
                }
            }

1 个答案:

答案 0 :(得分:2)

在这种情况下,我认为不需要分组。它可以通过选择和计算结果的行来完成。

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