BLTOOLKIT: Ele suporta iQueryable?
-
25-09-2019 - |
Pergunta
Geralmente com o Bltoolkit, busco dados do banco de dados da seguinte maneira:
using ( DbManager db = new MyDbManager() )
{
IList<MyObjects> objects = db
.SetCommand(query)//sometimes with additional parameters
.ExecuteList<MyObjects>()
;
}
Eu gostaria de ter capacidade de fazer o seguinte:
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
;
}
É possível alternar a solução que, com a modificação da linha de consulta orignal (modifique onde parte), mas às vezes é bastante complicado.
Existe alguma maneira fácil de fazer isso?
Obrigado. Qualquer pensamento é bem -vindo!
Solução
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
;
}
Outras dicas
Se você quiser ter um iQueriable, você deve usar o LINQ. BLTOOLKIT.LINQ e BLTOOLKIT.DATA.LINQQ DEVEM IMPORTAR.
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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow