When using Entity Framework Core, you may encounter a situation that needs to be filtered according to a specific attribute.
Working principle
Entity Framework Core 5 introduces the filter containing function, allowing you to specify the where, Orderby/Orderbydesmenting, TheNBYDEScending, SKIP, and take operators when you include related data.
Example
Consider the following code:
In this example, we filtered the results of the Include statement, including only the author's post. This is achieved by applying the where operational symbols to the collection.Precautions
<code class="language-csharp">using (var context = new BloggingContext()) { var blogs = context.Blogs .Include(blog => blog.Posts.Where(p => p.Author == "me")) // 过滤包含 .ToList(); }</code>
Each navigation attribute can only apply one filter. Some operations will accumulate results (for example,
will return customers with all orders).Include(c => c.Orders.Where(o => o.Name != "Foo")).Include(c => c.Orders.Where(o => o.Name == "Bar"))
Filter contains not affecting the Lord's inquiries (for example, o => o.Classification == c.Classification
The projection is neglected, whether it is filter or unique. Include(c => c.Orders.Where(o => o.IsDeleted))
The above is the detailed content of How Can I Filter Included Data in Entity Framework Core?. For more information, please follow other related articles on the PHP Chinese website!