简体   繁体   English

Linq to SQL立即执行查询

[英]Linq to SQL execute query immediately

I have Linq query and would like to execute it immediately and then manipulate data. 我有Linq查询,并希望立即执行它,然后操纵数据。 Now in the code below the first (1) Linq query is executed when second (2) Linq query is executed. 现在在下面的代码中,当执行第二(2)Linq查询时,执行第一个(1)Linq查询。 I would like to execute first(1) query first, how to do that? 我想首先执行first(1)查询,怎么做?

// 1
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 });


values = new int[interval];
labels = new string[interval];

for (var i = 0; i < labels.Length; i++)
{
    // 2
    var recordsCount = statistic.Count(p => p.Year == dayStep.Year && p.Month == dayStep.Month && p.Day == dayStep.Day);
}

I think you are missing an important concept in Data Manipulation in .NET 我认为你在.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. 需要强调的一点是,默认情况下,LINQ to SQL仅在您请求时才从数据库中检索数据,而不是在定义LINQ to SQL查询或创建Table集合时检索数据。 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. 当foreach循环启动时,LINQ to SQL创建并运行从LINQ to SQL查询派生的SQL SELECT语句,以创建ADO.NET DataReader对象。 Each iteration of the foreach loop performs the necessary GetXXX methods to fetch the data for that row. foreach循环的每次迭代都会执行必要的GetXXX方法来获取该行的数据。 After the final row has been fetched and processed by the foreach loop, LINQ to SQL closes the database connection. 在foreach循环提取并处理完最后一行之后,LINQ to SQL将关闭数据库连接。

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. 但是,如果要访问在SQL Server的远程实例上运行的数据库,则逐行提取数据并不能充分利用网络带宽。 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. 在这种情况下,您可以通过强制立即评估LINQ to SQL查询来获取和缓存单个网络请求中的所有数据。 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: 您可以通过调用ToList或ToArray扩展方法来执行此操作,这些方法在定义LINQ to SQL查询时将数据提取到列表或数组中,如下所示:

var productsQuery = from p in products.ToList()
select p;

Try this 1st statement (pay attention to .ToList() statement) 试试这第一个语句(注意.ToList()语句)

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();

您可以执行.ToList()之类的操作,它应该强制执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM