Linq查询中的子查询给出查询主体必须以select子句或group子句结束

时间:2015-02-18 12:32:45

标签: c# linq

预订 - 下面 - 应该是预订表中NumberBooked列的总和 - 它有一个指向TourDateId上TourDates表的链接。

但是我收到错误A query body must end with a select clause or a group clause

任何人都可以帮我解决这个疑问吗?

谢谢,

标记

var tours  = from t in Tours
   join d in TourDates on t.TourId equals d.TourId
   where d.Date == dt
      select new
        {
           t.TourId,
           d.TourDateId,
           booked = (from b in Bookings where d.TourDateId == b.TourDateId) 
                     Select new {bk.Sum(b.NumberBooked()} 
       };

2 个答案:

答案 0 :(得分:2)

我相信:

booked = (from b in Bookings where d.TourDateId == b.TourDateId) // oops
                 Select new {bk.Sum(b.NumberBooked()} 

应该是这样的:

booked = (from b in Bookings where d.TourDateId == b.TourDateId // move from here
                 select new {bk.Sum(b.NumberBooked()}) // to here

请注意,我移动了结束括号),使其位于选择之后,而不是TourDateId之后

答案 1 :(得分:1)

结束轮次副本结束了最后需要select的查询。

为什么不使用方法语法?在这种情况下,它的可读性要好得多。此外,Select是可选的.Where和方法语法:

  join d in TourDates on t.TourId equals d.TourId
  where d.Date == dt
  select new
  {
       t.TourId,
       d.TourDateId,
       booked = Bookings.Where(b => d.TourDateId == b.TourDateId)
                        .Sum(b => b.NumberBooked()) 
  };

请注意,我已删除了匿名类型,因为您只需要该列的总和

  

应该是Bookings表中NumberBooked列的总和 -   其中包含TourDateId上TourDates表的链接。