Filter Include
By filtering Include, you can reduce the data range returned by the navigation attribute during the query. This is especially useful when you need to filter according to the attributes containing the entity.
Example
Consider the following code:
This code contains all the posts and authors of each blog. To filter the result according to the author's name, you can use WHERE operation:
<code class="language-csharp">using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.ToList();
}</code>
Supported operations
<code class="language-csharp">using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.Where(blog => blog.Posts.Any(p => p.Author == "me")) // 修正后的Where条件
.ToList();
}</code>
EF Core 5 supports the following operations for filtering Include:
where
Orderby (Descending)/thenby (Deshenging)
- SKIP
take
-
Important Explanation
-
Each navigation attribute allows only one filter. -
Subsequent filtering Include will accumulate results.
Even if the delay loading is enabled, the filtering Include is considered loading.
Filter expression must be a "independent" predicate that can be used independently for filtering collection. -
- Filter Include and query filtering
- Filter Include will not affect the number of items returned by the main query. To filter the main query results, use a separate WHERE operation and INCLUDE.
- projection and filtering Include
projection usually ignores INCLUDE, regardless of whether they have been filtered. However, if the entity in the projection can be included, Include will be applied to it. (Note: In the original text, the description of projection and filtering Include is too brief, and the supplementary description is clearer here.)
The above is the detailed content of How Can I Filter Data Returned by Navigation Properties in EF Core Includes?. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn