CSharp · Console
// Entity Framework Plus
// Doc: https://entityframework-plus.net/query-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())
{
context.Filter<Post>(MyEnum.EnumValue1, q => q.Where(x => !x.IsSoftDeleted)).Disable();
// SELECT * FROM Post WHERE IsSoftDeleted = false
var list = context.Posts.Filter(MyEnum.EnumValue1).ToList();
FiddleHelper.WriteTable("After Filtering", list);
}
}
public static void GenerateData()
{
using (var context = new EntityContext())
{
context.Posts.Add(new Post() {Title = "Post_A", IsSoftDeleted = false, ViewCount = 12});
context.Posts.Add(new Post() {Title = "Post_B", IsSoftDeleted = true, ViewCount = 41});
context.Posts.Add(new Post() {Title = "Post_C", IsSoftDeleted = false, ViewCount = 24});
context.SaveChanges();
}
using (var context = new EntityContext())
{
FiddleHelper.WriteTable("Before Filtering", context.Posts.ToList());
}
}
public class EntityContext : DbContext
{
public EntityContext()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));
base.OnConfiguring(optionsBuilder);
}
public DbSet<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public bool IsSoftDeleted { get; set; }
public int ViewCount { get; set; }
}
public enum MyEnum
{
EnumValue1,
EnumValue2,
EnumValue3
}
}