Accepted answer

you're calling tolist on "sometable" right at the start. this is pulling the entire database table, with all rows and all columns, into memory and then performing all of the subsequent operations via linq-to-objects on that in-memory data structure.

not only are you suffering the penalty of transferring way more information across the network than you need to (which is slow), but c# isn't able to perform the operations nearly as efficiently as a database. that's partly because it looses access to any indexes, any database caching, any cached compiled queries, it isn't as efficient at dealing with data sets that large to begin with, and any higher level optimizations of the query itself (databases tend to do a lot of that).

next, you have a query inside of your groupby clause from f in db.a_lage_table where [...] that is performed for every row in the sequence. if the entire query is evaluated at the database level that would potentially be optimized, and even if it's not you're not going across the network to pass information (which is quite slow) for each record.


from p in db.sometable.tolist()

this basically says "get every record from sometable and put it in a list", without filtering at all. this is probably your problem.

Related Query

More Query from same tag