Linq离开加入“儿童无法评估”错误

时间:2015-10-21 11:42:55

标签: c# sql linq

我正在尝试在linQ中编写以下SQL查询:

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中进行调试时,我收到“无法评估子级”错误。知道我为什么会收到这个错误吗?

3 个答案:

答案 0 :(得分:2)

扩展我的评论,这不是错误。您的查询看起来很好,但它必须失败,因为ScrollViewDelegate在没有行匹配时返回默认值类型,因此您需要处理: -

DefaultIfEmpty

答案 1 :(得分: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};

答案 2 :(得分:0)

当我在您的DbSet实体中进行递归调用(例如包括设计不当的属性)时,我遇到了这个问题。

简而言之,如果您使用实体框架作为ORM,请查看数据库中的外键和主键关系。如果您的键关系不好,某些表将无法正常工作。