score:9
I think you are missing an important concept in Data Manipulation in .NET
Deferred and Immediate Fetching
An important point to emphasize is that by default, LINQ to SQL retrieves the data from the database only when you request it and not when you define a LINQ to SQL query or create a Table collection. This is known as deferred fetching.
When the foreach loop starts, LINQ to SQL creates and runs a SQL SELECT statement derived from the LINQ to SQL query to create an ADO.NET DataReader object. Each iteration of the foreach loop performs the necessary GetXXX methods to fetch the data for that row. After the final row has been fetched and processed by the foreach loop, LINQ to SQL closes the database connection.
Deferred fetching ensures that only the data an application actually uses is retrieved from the database. However, if you are accessing a database running on a remote instance of SQL Server, fetching data row by row does not make the best use of network bandwidth. In this scenario, you can fetch and cache all the data in a single network request by forcing immediate evaluation of the LINQ to SQL query. You can do this by calling the ToList or ToArray extension methods, which fetch the data into a list or array when you define the LINQ to SQL query, like this:
var productsQuery = from p in products.ToList()
select p;
score:3
Try this 1st statement (pay attention to .ToList()
statement)
var statistic = DataAccess.Instance.Statistics.Where(p => p.DateStamp >= fromDate && p.DateStamp <= DateTime.UtcNow && p.UserId == userId).
Select(p => new {p.DateStamp.Year, p.DateStamp.Month, p.DateStamp.Day }).ToList();
score:2
You can do something like .ToList() and it should force it to execute.
Source: stackoverflow.com
Related Articles
- Linq to SQL execute query immediately
- How to execute code as a part of the LINQ query
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Syntax to execute code block inside Linq query?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- How does linq actually execute the code to retrieve data from the data source?
- LINQ Source Code Available
- 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
- How to execute LINQ query for dictionary?
- How can I code a Linq query to do an upward Include?
- creating Linq to sqlite dbml from DbLinq source code
- 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
- Using LINQ query result for data source for GridControl c#
- LINQ .Where query takes 5+ minutes to execute
- Linq Query fail to execute method
- convert linq to object query to sql query (no linq to sql code or datacontext)
- Getting 'Data source is an invalid type' when binding Linq query to Gridview
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Converting foreach loop to LINQ query breaks code
- C# code for equivalent LINQ query
- How do I determine the source of unpredictable LINQ query results?
- Return all elements except for a specific element that also has no value
- How to get properties of a type inside an object
- How to write Linq query for fetching records
- linq where clause when id is in an array
- How can I convert each record from query result to arraylist c#
- ObjectMaterialize in EF not firing on first level query
- How can I write this with where clause where CreatedDate is in between Date1var & Date2var?
- Set values for entities
- How to write a LINQ join for two classes in ASP.NET MVC4
- Insert data into database from by selecting dropdown list
- get keys and vlaues from dictionary except "foreach"
- null issue during usage of count and sum LINQ
- c# Understanding AsEnumerable in LINQ query
- Like with or conditon in Linq
- Group and sub group with linq
- C# EF array intersect in LINQ to DB
- Split text with string
- StreamInsight query based on holding period
- Need to turn LINQ Query from Normal JOIN into RIGHT OUTER JOIN
- removing column and rows from DataTable using Linq