尝试使用多个记录进行左连接投影时出现 Linq 问题

发布于 2024-10-28 05:40:35 字数 1003 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相权↑美人 2024-11-04 05:40:35

我用空集合尝试了您的代码,但没有得到 NullReferenceException,因此实际上只有三种可能性:

  1. contextnull
  2. < code>context.EntityOnes 至少包含一个 null 对象
  3. 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:

  1. context is null
  2. context.EntityOnes contains at least one null object
  3. context.Comments contains at least one null object

If your database is really empty as you say, the only reason for the NullReferenceException can be reason 1.

柏林苍穹下 2024-11-04 05:40:35

也许我误解了您想要实现的目标,但 Linq 确实会自动遵循 1..n 关系并在对象中使用字段来表示它们。考虑以下结构:

Entity

Comment

其中 1 个 Entity 有许多 Comment
然后,在 Linq 中,表示 Entity 的对象有一个字段 Comments,其中包含所有注释的集合,这些注释的外键设置为实体的主键。这是 Linq 的默认行为。

因此,我认为您的代码应该是这样的:

var entitiesWithComments = context.Where(entity => entity.Comments.Count > 0);

现在您可以迭代集合 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 many Comments.
Then in Linq the object representing Entity has a field Comments, 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:

var entitiesWithComments = context.Where(entity => entity.Comments.Count > 0);

Now you can iterate over the collection entitiesWithComments and each object in it is an Entity with the field Comments which contains the comments to this entity.

仙女山的月亮 2024-11-04 05:40:35

我认为这个错误比你“调试”它的样子更平淡无奇。此 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文