过滤EF Core中的包含 [英] Filtering on Include in EF Core

查看:239
本文介绍了过滤EF Core中的包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试过滤初始查询。我已经将包含叶子的模型嵌套了。我正在尝试根据其中一个包含项的属性进行过滤。例如:

 使用(var context = new BloggingContext())
{
var blogs = context.Blogs
.include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.ToList();
}

我也怎么说。 > w.post.Author == me)

解决方案

最后,此功能从EF Core预览版本5.0.0-preview.3.20181.2和已实现在EF Core 5.0.0版中将为GA。请参阅EF核心的文档。 / p>

支持的操作:




  • 在哪里

  • OrderBy(降序)/ ThenBy(降序)

  • 跳过

  • 收取



一些用法示例(来自原始功能请求):


每次导航仅允许使用一个过滤器,因此对于同一导航需要多次(例如多次)然后在同一导航中包含),仅对该过滤器应用一次,或对该导航应用完全相同的过滤器。




 客户
.Include(c => c.Orders.Where(o => o.Name!= Foo)))。 (o => o.OrderDetails)
.Include(c => c.Orders).ThenInclude(o => o.Customer)

 客户
.Include(c => c.Orders.Where(o => o.Name!= Foo))。ThenInclude(o => o.OrderDetails)
.Include(c => c.Orders.Where (o => o.Name!= Foo))。ThenInclude(o => o.Customer)

另一个重要说明:


使用新过滤器操作包含的集合被视为已加载。


这意味着如果启用了延迟加载,则不会触发最后一个示例中的 customers.Orders 地址重新加载整个 Orders 集合。



另一方面,如果其他 Order 被加载到相同的上下文中,由于关系修正订单集合中有更多的可能被添加到 customers.Orders 集合中。 >。这是不可避免的,因为EF的变更跟踪器是如何工作的。



此外,两个后续过滤的 Include 将累积结果。例如...

  customers.Include(c => c.Orders.Where( o =>!o.IsDeleted))

...之后是...

  customers.Include(c => c.Orders.Where(o => o.IsDeleted) )

...将产生个客户包含所有订单的 Orders 集合。


I'm trying to filter on the initial query. I have nested include leafs off a model. I'm trying to filter based on a property on one of the includes. For example:

using (var context = new BloggingContext())
{
    var blogs = context.Blogs
        .Include(blog => blog.Posts)
            .ThenInclude(post => post.Author)
        .ToList();
}

How can I also say .Where(w => w.post.Author == "me")?

解决方案

Finally, this feature has been implemented starting with EF Core preview version 5.0.0-preview.3.20181.2 and will be GA in EF Core version 5.0.0. See EF-core's documentation.

Supported operations:

  • Where
  • OrderBy(Descending)/ThenBy(Descending)
  • Skip
  • Take

Some usage examples (from the original feature request):

Only one filter allowed per navigation, so for cases where the same navigation needs to be included multiple times (e.g. multiple ThenInclude on the same navigation) apply the filter only once, or apply exactly the same filter for that navigation.

customers
    .Include(c => c.Orders.Where(o => o.Name != "Foo")).ThenInclude(o => o.OrderDetails)
    .Include(c => c.Orders).ThenInclude(o => o.Customer)

or

customers
    .Include(c => c.Orders.Where(o => o.Name != "Foo")).ThenInclude(o => o.OrderDetails)
    .Include(c => c.Orders.Where(o => o.Name != "Foo")).ThenInclude(o => o.Customer)

Another important note:

Collections included using new filter operations are considered to be loaded.

That means that if lazy loading is enabled, addressing customers.Orders from the last example won't trigger a reload of the entire Orders collection.

On the other hand, if other Orders are loaded into the same context, more of them may get added to a customers.Orders collection because of relationship fixup. This is inevitable because of how EF's change tracker works.

Also, two subsequent filtered Includes in the same context will accumulate the results. For example...

customers.Include(c => c.Orders.Where(o => !o.IsDeleted))

...followed by...

customers.Include(c => c.Orders.Where(o => o.IsDeleted))

...will result in customers with Orders collections containing all orders.

这篇关于过滤EF Core中的包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆