CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
// The following configures EF to create a Sqlite database file as `C:\blogging.db`.
// For Mac or Linux, change this to `/tmp/blogging.db` or any other absolute path.
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer(@"Server=.\SQLEXPRESS;Database=SchoolDB;Trusted_Connection=True;");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; set; } = new List<Post>();
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
public class Program
{
public static void Main()
{
using var context = new BloggingContext();
var query = context.Set<Blog>()
.Where(b => b.Posts.Any(p => p.Title.Contains("abc")))
.Include(b => b.Posts.Where(p => p.Title.Contains("abc")))
.Take(10)
.AsQueryable();
var query2 = (Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable<Blog>)query;
Console.WriteLine(query2.DebugView.Query);
}
}