简体   繁体   English

对同一个Linq结果执行多个Linq查询

[英]Performing multiple Linq queries against the same Linq result

I have created a dashboard that all data displayed on it shares 4 common elements (startDate,endDate,CompanyID,StoreID) that are used as Where clauses in a Linq statement. 我创建了一个仪表板,该仪表板上显示的所有数据共享4个公共元素(startDate,endDate,CompanyID,StoreID),这些元素在Linq语句中用作Where子句。 The result of that statement is then queried in a variety of ways to group and sort the data and used in charts, lists etc. Here is a short snippit to show the duplication that is currently going on: 然后以多种方式查询该语句的结果,以对数据进行分组和排序,并在图表,列表等中使用。这是一个简短的摘录,以显示当前正在进行的重复:

var dashboardEntity = new BlueStreakSalesDWEntities();

  //Get Total Sales
ViewBag.companySalesTotal = dashboardEntity.FactSales.Where(d => d.DateKey >= startDate)
                                                     .Where(d => d.DateKey <= endDate)
                                                     .Where(c => c.CompanyID == companyID)
                                                     .Sum(a => a.Amount);

//get list of all items sold
var companyStoreTotalItem = dashboardEntity.FactSales.Where(d => d.DateKey >= startDate)
                                           .Where(d => d.DateKey <= endDate)
                                           .Where(c => c.CompanyID == companyID).GroupBy(m => new { m.Description })
                                           .Select(g => new DescriptionAmountModel { Amount = g.Sum(a => a.Amount).Value, Description = g.Key.Description })
                                           .OrderByDescending(x => x.Amount);

I have like 15 of these calls on the dashboard and it can get very slow at times from what I imagine are multiple calls when in reality the database only needs to be queried once then that result needs to be queried for different results. 我在仪表板上有15个这样的调用,它有时会变得很慢,而我想象的是多次调用,而实际上实际上只需要查询一次数据库,然后就需要为不同的结果查询结果。

How can I do this? 我怎样才能做到这一点?

Any help would be greatly appreciated 任何帮助将不胜感激

In your current solution each query executes separatly, on the same data. 在您当前的解决方案中,每个查询将对相同的数据分别执行。 You can first execute the shared parts of the queries and bring the results from database. 您可以先执行查询的共享部分,然后从数据库中获取结果。 In your examples it is these where conditions 在你的例子是这些where的条件

//Executes in database
var entities = dashboardEntity.FactSales.Where(d => d.DateKey >= startDate)
                                        .Where(d => d.DateKey <= endDate)
                                        .Where(c => c.CompanyID == companyID)
                                        .ToList();

Now that this data is filtered to only what you want you can in memory do the rest of the aggregations: 现在,已将这些数据过滤为仅在内存中所需的数据,可以执行其余的聚合:

//Happens in the List<T> in memory
ViewBag.companySalesTotal = entities.Sum(a => a.Amount);

var companyStoreTotalItem = entities.GroupBy(m => new { m.Description })
                                    .Select(g => new DescriptionAmountModel { Amount = g.Sum(a => a.Amount).Value, Description = g.Key.Description })
                                    .OrderByDescending(x => x.Amount);

This way you can make efficient. 这样您可以提高效率。 This make the query execute single time in database and rest of the part happen on the pullout in memory data 这使查询一次在数据库中执行,其余部分发生在内存数据中的提取上

var result = dashboardEntity.FactSales.Where(d => d.DateKey >= startDate && d => d.DateKey <= endDate && d.CompanyID == companyID).ToList();

ViewBag.companySalesTotal = result.Sum(a => a.Amount); 

//then get list of all items sold from in memory data
var companyStoreTotalItem = result.GroupBy(m => new { m.Description }).Select(g => new DescriptionAmountModel { Amount = g.Sum(a => a.Amount).Value, Description = g.Key.Description }).OrderByDescending(x => x.Amount);

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

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