Is it better DLinq over IQueryable or DLinq over IEnumerable for better performance?

StackOverflow https://stackoverflow.com/questions/18269771

  •  24-06-2022
  •  | 
  •  

Question

If I run against dlinq, for example

var custq = data.StoreDBHelper.DataContext.Customers as IEnumerable <Data.SO.Customer>;

I Thought it was not much of a difference against running:

var custq = data.StoreDBHelper.DataContext.Customers as IQueryable <Data.SO.Customer>;

As, IQueryable inherits from IEnumerable.

But I discovered the following, if you call: custq.Sum() then the program will process this as you called .toList() it you use the 'as IEnumerable' because the memory on the progam raised to the same level, when i tried, custq.ToList.Sum() but not on the 'as IQueryable' (because the issue then running on sql server) and did not affect the memory usage of the program.

My question is simply this, should you not use 'as IEnumerable' on Dlinq? But 'as IQueryable' as an general rule? I know that if you are running standard iterations, it gets the same result, between 'as IEnumerable'and 'as IQueryable'.

But is it just the summary functions and delegate expressions in where statement that there will be a difference - or will you in general get better performance if you use 'as IQueryable'? (for standard iteration and filter functions on DLinq entities)

Thanks !

Was it helpful?

Solution

Well, depends on what you want to do... Casting it as IEnumerable will return an object you can enumerate... and nothing more. So yes, if you call Count on an IEnumerable, then you enumerate the list (so you actually perform your Select query) and count each iteration.

On the other hand, if you keep an IQueryable, then you may enumerate it, but you could also perform database operations like Were, OrderBy or count. Then this will delay execution of the query and eventually modify it before running it.

Calling OrderBy on an enumerable browse all results and order them in memory. Calling OrderBy on a queryable simply adds ORDER BY at the end of your SQL and let database do the ordering.

In general, it is better to keep it as an IQueryable, yes... Unless you want to count them by actually browsing them (instead of doing a SELECT COUNT(*)...)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top