Home > Backend Development > C++ > How Can I Filter Included Data in Entity Framework Core?

How Can I Filter Included Data in Entity Framework Core?

Mary-Kate Olsen
Release: 2025-01-31 03:01:09
Original
417 people have browsed it

How Can I Filter Included Data in Entity Framework Core?

Entity Framework Core contains

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>
Copy after login

Each navigation attribute can only apply one filter. Some operations will accumulate results (for example,

will return customers with all orders).
  • The filter must use the predicate that can be used independently (for example,
  • ).
  • 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,
  • Return to all customers, not just those who have unpack orders).
  • o => o.Classification == c.Classification The projection is neglected, whether it is filter or unique.
  • However, when the physical projection contains the attributes that can be used, it will be applied. Include(c => c.Orders.Where(o => o.IsDeleted))
  • This Revised Response Maintains The Image and ITS Original Format, Rephrase Sentences for Improved Flow and Clarity, and Simplifies The Code Example for B ETTER Readability While Retaining The Original Meaning. En> Statement Directly, which is more confise and effect.

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!

Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template