尝试使用多个记录进行左连接投影时出现 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.
I'm trying to do a left join in linq to a table that has a one to many relationship. I need to set the collection property, but i can't make it to work
some example of my code is (i changed the entity names) :
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
};
When i'm retrieving the data for the query, i get a nullreferenceexception. the database is empty, but the DefaultIfEmpty should bring at least an empty collection, not return null
EntityOneComments is an IEnumerable<Comment>
I've also tried doing the last line like
EntityOneComments = tmpComments.ToList()
but to no avail, i got a strange error like this :
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我用空集合尝试了您的代码,但没有得到
NullReferenceException
,因此实际上只有三种可能性:context
为null
null
对象context.Comments
至少包含一个null
对象如果您的数据库确实是如您所说,空,
NullReferenceException
的唯一原因可能是原因 1。I tried your code with empty collections, and I didn't get a
NullReferenceException
, so there are really only three possibilities:context
isnull
context.EntityOnes
contains at least onenull
objectcontext.Comments
contains at least onenull
objectIf your database is really empty as you say, the only reason for the
NullReferenceException
can be reason 1.也许我误解了您想要实现的目标,但 Linq 确实会自动遵循 1..n 关系并在对象中使用字段来表示它们。考虑以下结构:
Entity
Comment
其中 1 个
Entity
有许多Comment
。然后,在 Linq 中,表示
Entity
的对象有一个字段Comments
,其中包含所有注释的集合,这些注释的外键设置为实体的主键。这是 Linq 的默认行为。因此,我认为您的代码应该是这样的:
现在您可以迭代集合
entitiesWithComments
,其中的每个对象都是一个带有字段Comments< 的
Entity
。 /code> 其中包含对此实体的注释。Maybe I'm misunderstanding what you are trying to achieve, but Linq does automaticaly follow 1..n relationships and makes Fields in the object to represent them. Consider this construction:
Entity
Comment
Where 1
Entity
has manyComment
s.Then in Linq the object representing
Entity
has a fieldComments
, which contains a collection of all the comments that have their foreign key set to the Entity's primary key. This is the default behaviour for Linq.Therefore I think that your code should be something like this:
Now you can iterate over the collection
entitiesWithComments
and each object in it is anEntity
with the fieldComments
which contains the comments to this entity.我认为这个错误比你“调试”它的样子更平淡无奇。此
DefaultIfEmpty
重写返回 null,因为 TSource 是引用类型,并且引用类型的默认值为 null。因此,错误在于该方法将生成 TSource 的“默认”实例的假设。I think the error is more prosaic than what you "debug" it to be. This
DefaultIfEmpty
override returns null because TSource is a reference type and default value for a reference type is null. So the error is in the assumption that the method will generate a "default" instance of the TSource.