EF で動的 where 句を生成するとなると、情報過多に陥ります。一部のソリューションは非常に古いように見えます (私は .NET 4.5 と EF 5 をターゲットにしています)。
ここに私が持っているものがあります:
public enum PersonTypes
{
Lazy = 1,
Awesome = 2,
SuperHero = 3
}
public bool IncludeLazyPeople { get; set; }
public bool IncludeAwesomePeople { get; set; }
public bool IncludeSuperHeroPeople { get; set; }
EF を使用して、提供された bool に一致する人物タイプを照会する必要があります。
私はこれを調べました: http://www.albahari.com/nutshell/predicatebuilder.aspx
そして、これが私が思いついたものです:
// create an expression that would include none of the person types
var personTypeExpression = PredicateBuilder.False<DAL.Models.Person>();
if (IncludeLazyPeople)
personTypeExpression = personTypeExpression.Or(person => person.PersonType == (int)PersonTypes.Lazy);
if (IncludeAwesomePeople)
personTypeExpression = personTypeExpression.Or(person => person.PersonType == (int)PersonTypes.Awesome);
if (IncludeSuperHeroPeople)
personTypeExpression = personTypeExpression.Or(person => person.PersonType == (int)PersonTypes.SuperHero);
// filter the people for all included types
var filteredPeople = ctx.People.Where(personTypeExpression);
AsExpandable 拡張機能が含まれていなかったため、これは機能しませんでした。それを試す前に、もっと簡単なアプローチがあるかどうか疑問に思っていますか?