BLToolKit:它支持IQueryable吗?

时间:2011-01-05 20:45:57

标签: c# .net .net-4.0 iqueryable bltoolkit

通常使用BLToolKit我会通过以下方式从DB获取数据:

using ( DbManager db = new MyDbManager() )
{
    IList<MyObjects> objects = db
        .SetCommand(query)//sometimes with additional parameters
        .ExecuteList<MyObjects>()
        ;
}

我希望能够做到以下几点:

using ( DbManager db = new MyDbManager() )
{
    IQueryable<MyObjects> qObjs = db
        .SetCommand(query)//sometimes with additional parameters
        .ExecuteQuery<MyObjects>()// here I don't want query actually to be executed
        ;

    // ... another logic, that could pass qObj into other part of program

    IList<MyObjects> objects = qObjs
        .Where(obj=>obj.SomeValue>=SomeLimit)    // here I want to put additional filters
        .ExecuteList()  // and only after that I wan't to execute query and fetch results
        ;
}

可以通过修改orignal查询字符串(修改WHERE部分)来解决这个问题,但有时它会非常复杂。

有没有简单的方法呢?

感谢。欢迎任何想法!

2 个答案:

答案 0 :(得分:2)

using ( DbManager db = new MyDbManager() )
{
    IQueryable<MyObjects> qObjs = 
        from p in db.GetTable<MyObjects>()
        //sometimes with additional parameters
        select p;

    // ... another logic, that could pass qObj into other part of program

    IList<MyObjects> objects = qObjs
        .Where(obj=>obj.SomeValue>=SomeLimit)    // here I want to put additional filters
        .ToList()  // and only after that I wan't to execute query and fetch results
        ;
}

答案 1 :(得分:0)

如果你想拥有IQueriable,你必须使用Linq。 BlToolkit.Linq和BlToolkit.Data.Linq必须导入。

        IQueryable<DataModel.Object> query  = db.Object;


        If ((int)cmb_somthing.SelectedValue) > 0 {

            int ID  = (int)cmb_somthing.SelectedValue;
            query = query.Where(m=> m.ID = ID);

        }
        query = query.Where(m=> m.Date >= StartDate &&
                                m.Date <= EndDate);