score:16
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.
score:8
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.
Source: stackoverflow.com
Related Articles
- LINQ Query takes too long
- Linq query in controller takes too long
- Linq query takes too long when using Func & OrderByDescending
- Linq query takes far too long
- Linq query over mysql table takes too long
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Linq query built in foreach loop always takes parameter value from last iteration
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Linq query taking too long
- linq query Take() takes precedence over Distinct()?
- LINQ Source Code Available
- Linq query runs in a fraction of a second, but .ToList() takes 3.5 seconds
- Linq 'contains' query taking too long
- linq - how do you do a query for items in one query source that are not in another one?
- How can I write the following code more elegantly using LINQ query syntax?
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- C# Linq query help removing foreach loops creating cleaner code
- Use a linq query as microsoft local report Data Source (WinForms)
- Determine the source DataContext for a Linq to Sql query
- LINQ query returns old results when source list is re-initialized
- How to get SQL query into LINQ form in C# code
- why this simple linq query takes so much time?
- How can I code a Linq query to do an upward Include?
- Convert string to long type and use in a linq query within asp.net MVC
- creating Linq to sqlite dbml from DbLinq source code
- linq grouping result takes a long time
- LINQ takes 40s to end query - Performance
- Identify source of linq to sql query
- NHibernate LINQ query performance, which code fragment is better?
- Linq sub query when using a repository pattern with EF code first
- Linq order by issue
- Flattening a tree of classes using SelectMany()
- Conditional "orderby" sort order in LINQ
- Displaying DateTime.Hour with AM or PM attached
- Linq join with array
- C# Sorting alphabetical order a - z and then to aa, ab - zz
- .NET LINQ IQueryable: what is `Expression` for?
- Performing a Contains within an Intersect
- ordering a list of addresses
- Datatable null to empty string
- linq join in ASP MVC
- Force Entity Framework to update several records in one round trip.
- Filter DataTable Efficiently
- VB.Net LINQ Get Field Names when no rows
- concatenate the results of a Linq query
- Can I break my report into groups with LINQ?
- How to return only collection of certain entries from a complex type after query?
- Linq with boolean function to relational db in Entity Framework
- Linq Extension method for Join
- How does Visual Studio evaluate the IEnumerable without breaking into its IEnumerator<T>'s MoveNext?