« LINQKit » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications
Ligne 11 : Ligne 11 :
// as x.Users is not a IQueryable, Any cannot take an Expression but a Func
// as x.Users is not a IQueryable, Any cannot take an Expression but a Func
// so we need to use Compile to get Func from Expression
// so we need to use Compile to get Func from Expression
context.Groups.Where(x => x.Users.Any(expression.Compile())); // Entity Framework will throw an exception
context.Groups.Where(x => x.Users.Where(expression.Compile())); // Entity Framework will throw an exception


// solution: use AsExpandable
// solution: use AsExpandable

Version du 14 mars 2021 à 11:09

Links

AsExpandable

Csharp.svg
Expression<Func<User, bool>> expression = x => x.Name.EndsWith("2");

context.Users.Where(expression); // works fine !

// as x.Users is not a IQueryable, Any cannot take an Expression but a Func
// so we need to use Compile to get Func from Expression
context.Groups.Where(x => x.Users.Where(expression.Compile())); // Entity Framework will throw an exception

// solution: use AsExpandable
context.Groups.AsExpandable().Where(x => x.Users.Any(expression.Compile()));

Installation

Bash.svg
dotnet add package LinqKit.Microsoft.EntityFrameworkCore