Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does LINQ perform slower when extracting results to a List?

Tags:

c#

linq

I have the following LINQ statement, and I'm finding that it's not running as quickly as I'd like. Are there other variations that run faster, or is there another way to do it that is faster?

var products = session.Products.Where(x => x.Supplier.Address.State == "HI").ToList();
like image 679
Rawhi Avatar asked Dec 06 '25 08:12

Rawhi


1 Answers

What you didn't state is that this was in a blog post for a specific LINQ provider. Let's put it in context:

And now query it:

using(var session=new Session()){
   var products = session.Products.Where(x => x.Supplier.Address.State == "HI")
                                  .ToList();
}

… and it works :). This is what’s called a “Deep Graph” query in the ObjectDatabase world and usually spells trouble for performance. To be honest I haven’t pegged this query yet over multiple records – but from what I’ve read this is normal for Mongo and it supports it quite well.

It's not the call to ToList which "usually spells trouble for performance" - it's the fact that you're doing a "deep graph query" in an object database. Yes, you're then fetching all the results into memory, but it's the nature of the store which is the cause for concern here.

like image 144
Jon Skeet Avatar answered Dec 07 '25 22:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!