disadvantages of using IQueryable !

2019-08-17 15:16发布

问题:

Is there are any performance issues related to using IQueryable ?
Besides, if I use a cursor instead of using IQueryable (is that better) .
IQueryable vs IEnumerable vs IList ?
I use MongoDB as my database . Thank you

回答1:

I don't know how the MongoDB C# binding works, but describe how it usually works:

When using IQueryable an expression tree is constructed, then translated into a format the database can understand and then executed in the database-server.

This typically has a small overhead(construct expression tree and translate it) compared to directly writing queries in the format the database understands.

With IEnumerable delegates are used, instead of expressions. It needs to iterate over the complete dataset and then filters using Linq-To-Objects. This is much slower.



回答2:

Those are just different interfaces, and as interfaces doesn't contain any implementation it doesn't matter for performance which you use.

When you loop over the values the enumerator of the actual class will be used, regardless of which interface you use to access it.