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:2
You can do something like .ToList() and it should force it to execute.
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();
Source: stackoverflow.com
Related Query
- Linq to SQL execute query immediately
- Determine the source DataContext for a Linq to Sql query
- How to get SQL query into LINQ form in C# code
- Identify source of linq to sql query
- convert linq to object query to sql query (no linq to sql code or datacontext)
- Linq to SQL FirstOrDefault does not execute the SQL query
- How can I optimise this LINQ query to only execute a single SQL command?
- How to execute code as a part of the LINQ query
- What SQL query or LINQ code would get the following data?
- Convert SQL update query to c# LINQ code
- LINQ execute SQL query with output parameter
- How are people unit testing code that uses Linq to SQL
- Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator
- LINQ to SQL query using "NOT IN"
- how to see generated sql from a linq query
- How to write linq query to match SQL like select top 100 * from tab?
- How do I most elegantly express left join with aggregate SQL as LINQ query
- LINQ To SQL exception: Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains operator
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Steps for a beginner to run very basic linq to sql query using Linqpad
- linq to sql recursive query
- Extract sql query from LINQ expressions
- Convert SQL to LINQ Query
- Syntax to execute code block inside Linq query?
- LINQ to SQL query not returning correct DateTime
- Calling a SQL User-defined function in a LINQ query
- How to force LINQ to SQL to evaluate the whole query in the database?
- Optimize LINQ query that runs fast in Sql server?
- linq to sql query with multiple where parameters
- Simple sql to Linq query with group by and aggregate functions
More Query from same tag
- How do I use LINQ to group & project objects, based on a member dictionary key?
- why ForEach Linq Extension on List rather than on IEnumerable
- I cant get the values from xml document
- Using QueryString to display Details
- Split a collection into n parts with LINQ, in VB.Net
- Checking for nulls in Linq Query
- LINQ to SQL - FROM X WHERE X = "1" SELECT Y
- Is an Implemenation Depending on Access to Modified Closure Undesirable?
- C# type arguments cannot be inferred from usage in Select with multiple returns
- Using Linq to query objects from a database and checking if they exit in a another list or array
- StackOverflowException on nested query, small item count
- Dynamically Sorting with LINQ
- Compare the values of properties of two objects in LINQ
- What might cause a Stack Overflow during linq iteration of Dictionary?
- One context reference LINQ query throws multiple references exception - BUG?
- How do I group by in LINQ using multiple criteria?
- linq ToArray<string>().Join(...) method is not working?
- LINQ to SQL Lookup table? Join perhaps? I'm lost... ;/
- Filter data from a virtual foreign key on an SQL query
- LINQ to check boolean value
- Find a Class object's Item with Maximum character count in a User Defined List
- How to write LINQ left join
- C# extracting ID from list of structures using LINQ
- List sorting WITH repetitions and LINQ
- LINQ Group by with multiple properties in VB.Net
- Linq To Entities Compare Value Against List<int>
- IEnumerable<string> to string
- TableName.RowName retrieves Nothing in MVC
- Linq to Entity Query takes more than 2 minutes to execute
- IsNumeric equivalent in Linq