根据方法参数添加限制

时间:2012-03-08 10:44:27

标签: nhibernate queryover

我有一个方法

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? 
}  

如果FilterWeek参数为true,我想在Where条件中添加一个额外的“&amp;&amp; r.xyz == 1”子句。如果FilterWeek为false,则查询完成。

我该怎么做?

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:1)

这个:

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();
    }