2010-10-14 13 views
8

Trochę utknąłem na tym. Zasadniczo mam metodę, która chcę zwrócić wyrażenie predykatu, które można użyć jako warunek Where. Myślę, że to, co muszę zrobić, jest podobne do tego: http://msdn.microsoft.com/en-us/library/bb882637.aspx, ale trochę utknąłem, co muszę zrobić.Jak dynamicznie budować i zwracać predykat linq na podstawie danych wprowadzanych przez użytkownika

Metoda:

private static Expression<Func<Conference, bool>> GetSearchPredicate(string keyword, int? venueId, string month, int year) 
{ 
    if (!String.IsNullOrEmpty(keyword)) 
    { 
     // Want the equivilent of .Where(x => (x.Title.Contains(keyword) || x.Description.Contains(keyword))); 
    } 
    if (venueId.HasValue) 
    { 
     // Some other predicate added... 
    } 

    return ?? 

} 

Przykład użycia:

var predicate = GetSearchPreducate(a,b,c,d); 
var x = Conferences.All().Where(predicate); 

muszę to oddzielenie tak, że mogę przekazać moje orzeczenie w moim repozytorium i używać go w innych miejscach.

Odpowiedz

10

Predykat jest tylko funkcją, która zwraca wartość logiczną.

Nie mogę tego przetestować, teraz, ale czy to nie działa?

private static Expression<Func<Conference, bool>> GetSearchPredicate(string keyword, int? venueId, string month, int year) 
{ 
    if (!String.IsNullOrEmpty(keyword)) 
    { 
     //return a filtering fonction 
     return (conf)=> conf.Title.Contains(keyword) || Description.Contains(keyword))); 
    } 
    if (venueId.HasValue) 
    { 
     // Some other predicate added... 
     return (conf)=> /*something boolean here */; 
    } 

    //no matching predicate, just return a predicate that is always true, to list everything 
    return (conf) => true; 

} 

EDIT: Na podstawie wypowiedzi Matta Jeśli chcą komponować delegatów, można postępować w ten sposób

private static Expression<Func<Conference, bool>> GetSearchPredicate(string keyword, int? venueId, string month, int year) 
{ 
    Expression<Func<Conference, bool> keywordPred = (conf) => true; 
    Expression<Func<Conference, bool> venuePred = (conf) => true; 
    //and so on ... 


    if (!String.IsNullOrEmpty(keyword)) 
    { 
     //edit the filtering fonction 
     keywordPred = (conf)=> conf.Title.Contains(keyword) || Description.Contains(keyword))); 
    } 
    if (venueId.HasValue) 
    { 
     // Some other predicate added... 
     venuePred = (conf)=> /*something boolean here */; 
    } 

    //return a new predicate based on a combination of those predicates 
    //I group it all with AND, but another method could use OR 
    return (conf) => (keywordPred(conf) && venuePred(conf) /* and do on ...*/); 

} 
+1

Tak, właśnie lubię to. Po prostu wyodrębnij część (x => coś) i zapisz ją w wyrażeniu >. – Euphoric

+0

Pozdrawiam, ale chcę rozbudować filtr/predykat. Więc jeśli słowo kluczowe zostanie przekazane, dodaj filtr do tego. A jeśli przekazany został również obiekt, dodaj filtr do tego ... To tam wszystko mnie myli ... –

+0

A kiedy użyjesz wyrażenia, w którym potrzebujesz predykatu, użyj polecenia exp.Compile() – Les

Powiązane problemy