简体   繁体   中英

Entity Framework Query filters implementation for best perfomance

As the title says, I´m using entity framework 4.0 for a financial application. I have a winform where I list all the cheques (checks) I have. But in that form, the user can specify some filters.

在此处输入图片说明

If the user does not apply any filter, I just can make the query like this:

lista_cheques = db.Cheque.Include("Operacion").Include("Cliente").ToList();
datagridview.Datasource = lista_cheques;

That is simple. But when it applies filters, the problem gets bigger.

As you see, the user can use filter to see cheques (checks) of a specific client, dates, bank, CUIT number, check state, etc.

Now, my question is related to performance in the queries.

I was thinking on doing the filters separeted, like this:

lista_cheques = db.Cheque.Include("Operacion").Include("Cliente").Where(x => x.fecha_deposito == fecha).ToList();
lista_cheques = lista_cheques.Where(x => x.banco.id_banco = banco).ToList();
lista_cheques = lista_cheques.Where(x => x.Operacion.Cliente.id_cliente = id_cliente).ToList();

Translation: fecha is date Operacion is a group of checks Cliente is client.

In this way, I´m doing a query, then, a query from that query result, then a new query from that new result and that goes on.

I think this way might have a big performance issues. I know that SQL server optimize queries. So, if I´m doing fragmented queries, the optimizer is not working properly.

The other way I thought about but it´s very tedious, is to create one big query to handle every possible filter selection.

For example, the other example would be like this:

lista_cheques = db.Cheque.Include("Operacion").Include("Cliente").Where(x => x.fecha_deposito == fecha && x.banco.id_banco = banco && x.Operacion.Cliente.id_cliente = id_cliente).ToList();

The big problem is that I will need lots of combinations to be able to handle all the filter posibilities.

Ok guys, now, will I have performace issues in the first code example? I doing there one big query to the database, and then I´m doing the query in the list of objects (which I think will be faster). I´m pretty new to this ORM, and this listing will have to handle a lot of registries..

I somebody can give me some advice? I made pretty much a mess explaining, hope you can understand..

lista_cheques = db.Cheque.Include("Operacion").Include("Cliente").Where(x => x.fecha_deposito == fecha).ToList();
lista_cheques = lista_cheques.Where(x => x.banco.id_banco = banco).ToList();
lista_cheques = lista_cheques.Where(x => x.Operacion.Cliente.id_cliente = id_cliente).ToList();

Nearly perfect. Kill all those ToList and it is good.

The ToList means SQL is evaluated, so if all 3 flters trigger, 2 and 3 are evaluated in memory.

Kick the ToList away, and the different Where clauses get combined on the database.

Standard LINQ 101. Works like a charm and is always nice to see.

Then add as LAST line:

lista_cheques = lista_cheques.ToList ();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM