2015-10-21 26 views
1

我試圖寫在LINQ下面的SQL查詢:LINQ到左加入讓「孩子無法評價」的錯誤

select b.Title, b.Author from CurrentRented c left join Book b on c.BookId=b.BookId 
where c.UserId=2 

我的LINQ是:

var BooksRented = from c in db.CurrentRenteds 
        join b in db.Books on c.BookId equals b.BookId into bk 
        from b in bk.DefaultIfEmpty() 
        where c.UserId.Equals(UserID) 
        select new { 
         b.Title, b.Author, c.RentDate, c.ReturnDate}; 

然而,當我在Visual Studio 2010中進行調試時,出現「兒童無法評估」錯誤。任何想法,爲什麼我得到這個錯誤?

+0

這不是一個錯誤。您無法從Visual Studio調試linq查詢。 http://stackoverflow.com/questions/12456216/debugging-linq-children-could-not-be-evaluated –

+0

好的,我會用LinqPad。但是我的linQ並不好。任何想法爲什麼? – user3762810

回答

1

擴展我的評論,這不是一個錯誤。您查詢看起來很好,但一定要失敗,因爲類型的DefaultIfEmpty返回默認值時,沒有相匹配的行,所以你需要處理的是: -

from b in bk.DefaultIfEmpty() 
where c.UserId.Equals(UserID) 
select new { 
       Title = b != null ? b.Title : "", 
       Author = b!= null ? b.Author : "", 
       RentDate = c.RentDate, 
       ReturnDate = c.ReturnDate 
      }; 
0

你必須給在DefaultIfEmpty不同的別名,看下面。

var BooksRented = from c in db.CurrentRenteds 
        join b in db.Books on c.BookId equals b.BookId into bk 
        from d in bk.DefaultIfEmpty() 
        where c.UserId.Equals(UserID) 
        select new { 
         d.Title, d.Author, c.RentDate, c.ReturnDate};