Clauses Where multiples dans les expressions Lambda

J’ai une expression lambda simple qui va quelque chose comme ceci:

x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != Ssortingng.Empty) 

Maintenant, si je veux append une autre clause where à l’expression, par exemple, l.InternalName != Ssortingng.Empty alors quelle serait l’expression?

Peut être

 x => x.Lists.Include(l => l.Title) .Where(l => l.Title != Ssortingng.Empty && l.InternalName != Ssortingng.Empty) 

ou

 x => x.Lists.Include(l => l.Title) .Where(l => l.Title != Ssortingng.Empty) .Where(l => l.InternalName != Ssortingng.Empty) 

Lorsque vous regardez Where implémentation, vous pouvez voir qu’il accepte un Func(T, bool) ; cela signifie:

  • T est votre type IEnumerable
  • bool signifie qu’il doit retourner une valeur booléenne

Alors, quand tu le fais

 .Where(l => l.InternalName != Ssortingng.Empty) // ^ ^---------- boolean part // |------------------------------ "T" part 

Le lambda que vous passez à Where peut inclure n’importe quel code C # normal, par exemple l’opérateur && :

 .Where(l => l.Title != ssortingng.Empty && l.InternalName != ssortingng.Empty) 

Vous pouvez l’inclure dans la même instruction where avec l’opérateur && …

 x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != Ssortingng.Empty && l.InternalName != Ssortingng.Empty) 

Vous pouvez utiliser n’importe quel opérateur de comparaison (pensez à le faire comme une instruction if) tel que …

 List nums = new List(); nums.Add(3); nums.Add(10); nums.Add(5); var results = nums.Where(x => x == 3 || x == 10); 

… ramènerait 3 et 10.

Peut être

 x=> x.Lists.Include(l => l.Title) .Where(l => l.Title != ssortingng.Empty) .Where(l => l.InternalName != ssortingng.Empty) 

?

Vous pouvez probablement aussi le mettre dans la même clause where:

 x=> x.Lists.Include(l => l.Title) .Where(l => l.Title != ssortingng.Empty && l.InternalName != ssortingng.Empty) 
 x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != Ssortingng.Empty).Where(l => l.Internal NAme != Ssortingng.Empty) 

ou

 x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != Ssortingng.Empty && l.Internal NAme != Ssortingng.Empty)