Depending on how sophisticated the NHLinq implementation is, you may
need to run an expression visitor which visits the expression tree and
simplifies all of the method calls. You can get such a project here:
http://tomasp.net/blog/linq-expand.aspx
Bill Barry wrote:
> private static Expression<Func<Student, bool>>
> ProcessCriterias(List<SearchCriteria.StringValue> clist) {
> return clist.Select(item => ProcessCriteria(item)).Aggregate(Or);
> }
>
> private static Expression<Func<T, bool>> Or<T>(Expression<Func<T,
> bool>> expr1,
> Expression<Func<T,
> bool>> expr2) {
> var invokedExpr = Expression.Invoke(expr2,
> expr1.Parameters.Cast<Expression>());
> return Expression.Lambda<Func<T, bool>>
> (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
> }
>
>
> This is probably not the most efficient way to do any of this. The
> criteria API would be considerably better I think.
>
> If you do go this route you may also want an and function:
>
> private static Expression<Func<T, bool>> And<T>(Expression<Func<T,
> bool>> expr1,
> Expression<Func<T,
> bool>> expr2) {
> var invokedExpr = Expression.Invoke(expr2,
> expr1.Parameters.Cast<Expression>());
> return Expression.Lambda<Func<T, bool>>
> (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
> }
>
>
> lukefrice wrote:
>> Basically I am doing it like this.
>>
>> private static Expression<Func<Student, bool>> ProcessCriteria
>> (SearchCriteria.StringValue criteria)
>> {
>> if (criteria.FieldName == "FirstName") { return
>> BuildFirstName(criteria.Value, criteria.Comparer); }
>> }
>>
>> private static Expression<Func< Student, bool>> BuildFirstName(string
>> value, StringFilterType comparer)
>> {
>> switch (comparer)
>> {
>> case StringFilterType.Contains:
>> return (s) => s.FirstName.Contains(value);
>> case StringFilterType.EndsWith:
>> return (s) => s.FirstName.EndsWith(value);
>> case StringFilterType.Equals:
>> return (s) => s.FirstName == value;
>> default:
>> return (s) => s.FirstName.StartsWith(value);
>> }
>> }
>>
>> This way when i call into the class containing this, i can pass in
>> just the field name and the comparer type and it knows which set of
>> comparers.
>> After this I am combining the Expressions together with this inside my
>> dataProvider.
>>
>> var query = _session.Linq<Student>().AsQueryable();
>> query = StudentSearchCriteriaManager.BuildQuery(query,
>> criteria);
>> return query.ToList();
>>
>> and here is part of the BuildQuery method used to put them together
>>
>> result = result.Where(ProcessCriteria((SearchCriteria.NumericValue)
>> item));
>> break;
>>
>>
>>
>> On Aug 10, 5:09 pm, John Rayner <[email protected]> wrote:
>>
>>> Create your own Disjunction object?
>>>
>>> Disjunction disj = new Disjunction();
>>> for(int i = 0; i < numRestrictions; i++)
>>> disj.Add( Restrictions.Eq( .... ) );
>>>
>>> If not this, then please give an example of the sort of code you'd
>>> like to write.
>>>
>>> Cheers,
>>> John
>>>
>>> On Aug 10, 8:15 pm, lukefrice <[email protected]> wrote:
>>>
>>>
>>>
>>>
>>>> I do understand, but I am looking more for something like
>>>> PredicateBuilder. Is there anything like that that can be used with
>>>> NHibernate?
>>>>
>>>> On Aug 10, 1:23 pm, Tuna Toksoz <[email protected]> wrote:
>>>>
>>>>> this is programatic, you have as much flexibility as your imagination.
>>>>> ICriterion x=something;
>>>>> if(something)
>>>>> x=Restrictions.Or(x,anotherRestriction);
>>>>>
>>>>> if(another)
>>>>> x=Restrictions.Or(x,anotherthinghere);
>>>>>
>>>>> Can I tell what I mean?
>>>>>
>>>>> Tuna Toksöz
>>>>> Eternal sunshine of the open source mind.
>>>>>
>>>>> http://devlicio.us/blogs/tuna_toksozhttp://tunatoksoz.comhttp://twitt...
>>>>>
>>>>> On Mon, Aug 10, 2009 at 9:20 PM, lukefrice <[email protected]> wrote:
>>>>>
>>>>>> way to combine some of them as OR's, or po- Hide quoted text -
>>>>>>
>>>> - Show quoted text -
>>>>
>> >>
>>
>>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---