左连接查询LinqToSql的问题

时间:2011-07-05 11:38:26

标签: sql linq-to-sql join left-join

      IBookingRepository bookingResp = new BookingRepository();
        IQueryable<bookingTest> bookings = bookingResp.GetAllBookingsByView();
       var grid = new System.Web.UI.WebControls.GridView();
        grid.DataSource = from booking in bookings
                        join f in getallAttendees on booking.UserID equals f.UserID into fg
                        from fgi in fg.DefaultIfEmpty() //Where(f => f.EventID == booking.EventID)
                        where
                              booking.EventID == id


                          select new
                          {
                              EventID = booking.EventID,

                              UserID = booking.UserID,
                              TrackName = booking.Name,
                              BookingStatus = booking.StatusID,
                               AttendeeName = booking.FirstName,
                             // name = account.FirstName,
                              AmountPaid = booking.Cost,
                              AttendeeAddress = booking.DeliveryAdd1,
                              City = booking.DeliveryCity,
                               Postcode = booking.Postcode,
                              Date = booking.DateAdded,
                              hel = fgi == null ? null : fgi.HelmetsPurchased }// Product table 

嗨,上面的查询没有执行它会给出错误:指定的LINQ表达式包含对与不同上下文关联的查询的引用。任何人都可以发现查询的问题。

1 个答案:

答案 0 :(得分:1)

我认为您的getAllAttendees来自与bookings不同的背景,因此您将无法加入它们。要提供更准确的答案,您需要显示bookingsgetAllAttendees的来源。