EF.Plus - Query IncludeFilter - Load multiple levels (EF Core)

CSharp · Console

/Program.cs

// Entity Framework Plus
// Doc: https://entityframework-plus.net/query-include-filter

// @nuget: Microsoft.EntityFrameworkCore.SqlServer
// @nuget: Z.EntityFramework.Plus.EFCore

using System;
using System.Linq;
using Z.EntityFramework.Plus;
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		using (var context = new EntityContext())
		{
			context.Database.EnsureCreated();
		}
		
		GenerateData();
 
        // Update: UPDATE all rows from the database using a LINQ Query without loading entities in the context.
        using (var context = new EntityContext())
        {
            // LOAD blogs and related active posts and comments.
            var list = context.Blogs.IncludeFilter(x => x.Posts.Where(y => !y.IsSoftDeleted))
                .IncludeFilter(x => x.Posts.Where(y => !y.IsSoftDeleted)
                    .SelectMany(y => y.Comments.Where(z => !z.IsSoftDeleted)))
                .ToList();

            foreach (var blog in list)
            {
                Console.WriteLine("Active posts in {0}: {1}", blog.Title, blog.Posts.Count());

                foreach (var post in blog.Posts)
                {
                    Console.WriteLine("\t {0} : Active comments ({1})", post.Content, post.Comments.Count);
                }
            }

            int i = 0;

            //FiddleHelper.WriteTable("After Filtering", list);
        }
    }

    public static void GenerateData()
    {

        using (var context = new EntityContext())
        {
            context.Blogs.Add(new Blog() { Title = "Blog_A", IsSoftDeleted = true });
            context.Blogs.Add(new Blog() { Title = "Blog_B", IsSoftDeleted = true });

            context.SaveChanges();

            context.Posts.Add(new Post() { Content = "Post 1 in Blog_A", Date = DateTime.Now.AddDays(-4), IsSoftDeleted = false, Price = 35, BlogId = 1 });
            context.Posts.Add(new Post() { Content = "Post 2 in Blog_A", Date = DateTime.Now.AddDays(-4), IsSoftDeleted = true, Price = 22, BlogId = 1 });
            context.Posts.Add(new Post() { Content = "Post 1 in Blog_B", Date = DateTime.Now.AddDays(-5), IsSoftDeleted = false, Price = 37, BlogId = 2 });
            context.Posts.Add(new Post() { Content = "Post 2 in Blog_B", Date = DateTime.Now.AddDays(-5), IsSoftDeleted = false, Price = 7, BlogId = 2 });

            context.SaveChanges();

            context.Comments.Add(new Comment() { Text = "Comment 1 in post 1", IsSoftDeleted = false, PostId = 1 });
            context.Comments.Add(new Comment() { Text = "Comment 2 in post 1", IsSoftDeleted = false, PostId = 1 });
            context.Comments.Add(new Comment() { Text = "Comment 1 in post 3", IsSoftDeleted = true, PostId = 3 });
            context.Comments.Add(new Comment() { Text = "Comment 2 in post 3", IsSoftDeleted = false, PostId = 3 });

            context.SaveChanges();
        }
    }

	public class EntityContext : DbContext
	{
		public EntityContext() 
		{
		}

		protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
		{
			optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));

			base.OnConfiguring(optionsBuilder);
		}
		
		public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
        public DbSet<Comment> Comments { get; set; }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Title { get; set; }
        public bool IsSoftDeleted { get; set; }
        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Content { get; set; }
        public DateTime Date { get; set; }
        public bool IsSoftDeleted { get; set; }
        public int Price { get; set; }
        public int BlogId { get; set; }
        public List<Comment> Comments { get; set; }
    }

    public class Comment
    {
        public int CommentId { get; set; }
        public string Text { get; set; }
        public bool IsSoftDeleted { get; set; }
        public int PostId { get; set; }
    }
}
;