increddibelly
increddibelly

Reputation: 1239

Add restriction based on method parameter

I have a method

List<MyType> DoQuery(bool FilterWeek) {  

    var result = session.QueryOver<MyType>()  
                        .Where (r => r.isValid == 1  
                                && r.value1 == 2
                                && r.name == "XYZ"
                                && [...etc, more columns are used...]  
                               ) 
                        // how do I go on from this point? 
}  

if the FilterWeek parameter is true, I want to add an extra "&& r.xyz == 1" clause to the Where criteria. If FilterWeek is false, the query is done.

How do I do that?

Upvotes: 1

Views: 222

Answers (2)

Fourth
Fourth

Reputation: 9351

this:

List<MyType> DoQuery(bool FilterWeek) {  

        var result = session.QueryOver<MyType>()  
                            .Where (r => r.isValid == 1  
                                    && r.value1 == 2
                                    && r.name == "XYZ"
                                    && [...etc, more columns are used...]  
                                   );

        if(FilterWeek)
           result.Where(x => x.Whatever == 1)

        //the query won't get executed until here
        result.List();
    }  

Upvotes: 1

Diego Mijelshon
Diego Mijelshon

Reputation: 52735

if (FilterWeek)
    result = result.Where(r => r.xyz ==1);

//...whenever you're done, execute the query using List() or SingleOrDefault()

Upvotes: 4

Related Questions