我试图做一个左连接linq到一个有一对多关系的表。我需要设置集合属性,但我不能让它工作
我的代码的例子是(我改变了实体名称):Linq问题当尝试左多个记录的连接投影
context =>
from entity1 in context.EntityOnes
join comment in context.Comments on entity1.Id equals comment.CommentSourceId into tmpComments
from comment in tmpComments.DefaultIfEmpty()
select new EntityOneData
{
EntityOne = entity1,
EntityOneComments = tmpComments
};
当我检索了查询的数据,我收到nullreferenceexception。数据库是空的,但DefaultIfEmpty应携带至少一个空的集合,而不是返回null
EntityOneComments is an IEnumerable<Comment>
我也试着做最后一行像
EntityOneComments = tmpComments.ToList()
但无济于事,我有一个奇怪的错误是这样的:
LINQ to Entities does not recognize the method 'System.Collections.Generic.List1[RecipeCategoryItem] ToList[RecipeCategoryItem](System.Collections.Generic.IEnumerable1[RecipeCategoryItem])' method, and this method cannot be translated into a store expression.
你意识到,你使用变量名'comment'两次,两个不同的东西?也许这就是问题所在? – 2011-03-31 06:50:22
对不起?你什么意思?我不明白 – 2011-03-31 06:52:44
'在上下文中加入评论。注释'和'来自tmpComments.DefaultIfEmpty()'中的注释。您正在使用变量名称'comment'两次。但是,我不确定这是否是问题。 – 2011-03-31 06:53:49