Created
September 18, 2019 22:26
-
-
Save M-Yankov/f664bc0458f0deed0c39b0e91b2ebadf to your computer and use it in GitHub Desktop.
A sample C# .NET CORE app using PredicateBuilder.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<configuration> | |
<connectionStrings> | |
<add name="LocalDb" connectionString="Data Source=LENOVO-G710;Initial Catalog=PredicateBuilder;Integrated Security=True" providerName=""/> | |
</connectionStrings> | |
</configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp2.2</TargetFramework> | |
<LangVersion>7.3</LangVersion> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="LinqKit.Microsoft.EntityFrameworkCore" Version="1.1.16" /> | |
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.6" /> | |
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.6" /> | |
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" /> | |
</ItemGroup> | |
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.Configuration; | |
using System.Threading.Tasks; | |
using LinqKit; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.EntityFrameworkCore.SqlServer; | |
namespace EFPredicateBuilder | |
{ | |
internal class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
// await new MyDbContext().Database.EnsureCreatedAsync(); | |
var filters = new List<FilterModel>() | |
{ | |
new FilterModel() { Id = 2, Value = "16"}, | |
new FilterModel() { Id = 1, Value = "300"}, | |
new FilterModel() { Id = 1, Value = "214"} | |
}; | |
var conditions = PredicateBuilder.New<MyModel>(); | |
foreach (var filter in filters) | |
{ | |
switch (filter.Id) | |
{ | |
case 1: | |
conditions = conditions.Or(x => x.Field1 == filter.Value); | |
break; | |
case 2: | |
conditions = conditions.Or(x => x.Field2 == filter.Value); | |
break; | |
default: | |
break; | |
} | |
} | |
conditions = conditions.And(x => x.Status == 2); | |
List<MyModel> data = await new MyDbContext() | |
.Set<MyModel>() | |
.Where(conditions) | |
.AsNoTracking() | |
.ToListAsync(); | |
foreach (var item in data) | |
{ | |
System.Console.WriteLine($"{item.Id} -{item.Field1 ?? "NULL"} - {item.Field2 ?? "NULL"}"); | |
} | |
} | |
} | |
public class FilterModel | |
{ | |
public int Id { get; set; } | |
public string Value { get; set; } | |
} | |
public class MyDbContext : DbContext | |
{ | |
public DbSet<MyModel> Models { get; set; } | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => | |
optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["LocalDb"].ConnectionString); | |
} | |
public class MyModel | |
{ | |
[Key] | |
public int Id { get; set; } | |
public string Field1 { get; set; } | |
public string Field2 { get; set; } | |
public string Field3 { get; set; } | |
public string Field4 { get; set; } | |
public string Field5 { get; set; } | |
public string Field6 { get; set; } | |
public string Field7 { get; set; } | |
public string Field8 { get; set; } | |
public string Field9 { get; set; } | |
public string Field10 { get; set; } | |
public int Status { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment