« LINQKit » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications
 
(8 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
[[Category:.NET Core]]
[[Category:.NET Application]]
= Links =
= Links =
* [https://github.com/scottksmith95/LINQKit LINQKit] on GitHub
* [https://github.com/scottksmith95/LINQKit LINQKit] on GitHub


= AsExpandable =
= [https://github.com/scottksmith95/LINQKit#plugging-expressions-into-entitysets--entitycollections-the-solution AsExpandable] =
<kode lang='csharp'>
<kode lang='csharp'>
Expression<Func<User, bool>> expression = x => x.Name.EndsWith("2");
Expression<Func<User, bool>> expression = x => x.Name.EndsWith("2");
Ligne 15 : Ligne 15 :
// solution: use AsExpandable
// solution: use AsExpandable
context.Groups.AsExpandable().Where(x => x.Users.Any(expression.Compile()));
context.Groups.AsExpandable().Where(x => x.Users.Any(expression.Compile()));
// Compile never actually runs; AsExpandable will strip out the call to Compile and substitute it with a correct expression tree
</kode>
</kode>


= Installation =
= [https://github.com/scottksmith95/LINQKit#combining-expressions Combining Expressions] =
== Boolean operations ==
<kode lang='csharp'>
Expression<Func<User, bool>> e1 = x => x.Name.StartsWith("U");
Expression<Func<User, bool>> e2 = x => x.Name.EndsWith("2");
 
Expression<Func<User, bool>> e3 = x => e1.Invoke(x) && e2.Invoke(x);
 
Console.WriteLine(e3.Expand());  // x => (x.Name.StartsWith("U") AndAlso x.Name.EndsWith("2"))
 
context.Users.Where(e3.Expand());
</kode>
 
== Filter operation ==
<kode lang='csharp'>
Expression<Func<IEnumerable<int>, IEnumerable<int>>> e1 = l => l.Select(x => x + 1);
Expression<Func<IEnumerable<int>, IEnumerable<int>>> e10 = l => l.Select(x => x + 10);
 
Expression<Func<IEnumerable<int>, IEnumerable<int>>> combinedExpresions = x => e10.Invoke(e1.Invoke(x));
 
Console.WriteLine(combinedExpresions.Expand());  // x => (x.Select(x => x + 1).Select(x => x + 10))
var list = new[] { 1, 2, 3 };
var result = combinedExp.Invoke(list).ToList();  // 12, 13, 14
</kode>
 
= [https://github.com/scottksmith95/LINQKit#predicatebuilder PredicateBuilder] =
Allow to dynamically construct a lambda expression tree.
<kode lang='csharp'>
var predicate = PredicateBuilder.New<User>();
predicate = predicate.And(x => x.Name.StartsWith("U"));
predicate = predicate.And(x => x.Name.EndsWith("2"));
 
context.Users.Where(predicate);
</kode>
 
= [https://www.nuget.org/packages/LinqKit.Microsoft.EntityFrameworkCore/ Installation] =
<kode lang='bash'>
<kode lang='bash'>
dotnet add package LinqKit.Microsoft.EntityFrameworkCore
dotnet add package LinqKit.Microsoft.EntityFrameworkCore
</kode>
</kode>

Dernière version du 12 mars 2023 à 09:38

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()));
// Compile never actually runs; AsExpandable will strip out the call to Compile and substitute it with a correct expression tree

Combining Expressions

Boolean operations

Csharp.svg
Expression<Func<User, bool>> e1 = x => x.Name.StartsWith("U");
Expression<Func<User, bool>> e2 = x => x.Name.EndsWith("2");

Expression<Func<User, bool>> e3 = x => e1.Invoke(x) && e2.Invoke(x);

Console.WriteLine(e3.Expand());  // x => (x.Name.StartsWith("U") AndAlso x.Name.EndsWith("2"))

context.Users.Where(e3.Expand());

Filter operation

Csharp.svg
Expression<Func<IEnumerable<int>, IEnumerable<int>>> e1 = l => l.Select(x => x + 1);
Expression<Func<IEnumerable<int>, IEnumerable<int>>> e10 = l => l.Select(x => x + 10);

Expression<Func<IEnumerable<int>, IEnumerable<int>>> combinedExpresions = x => e10.Invoke(e1.Invoke(x));

Console.WriteLine(combinedExpresions.Expand());  // x => (x.Select(x => x + 1).Select(x => x + 10))
var list = new[] { 1, 2, 3 };
var result = combinedExp.Invoke(list).ToList();  // 12, 13, 14

PredicateBuilder

Allow to dynamically construct a lambda expression tree.

Csharp.svg
var predicate = PredicateBuilder.New<User>();
predicate = predicate.And(x => x.Name.StartsWith("U"));
predicate = predicate.And(x => x.Name.EndsWith("2"));

context.Users.Where(predicate);

Installation

Bash.svg
dotnet add package LinqKit.Microsoft.EntityFrameworkCore