我正在对一个更大的表进行这个linq查询,在这个表中,我对几个列求和,需要很长时间
var returnList = context.Revenue_Consolidate.Select(b => new { b.field, b.GrsVol, b.Type, b.OwnNetVol, b.OwnNetVal, b.UnitPrice })
.Where(r => r.FromProdDate > startDate && r.FromProdDate <= endDate)
.GroupBy(b => b.FromProdDate.Month)
.Select(r => new MiddelRQTGrid
{
Month = r.Select(b => b.FromProdDate.Month).FirstOrDefault(),
Year = r.Select(b => b.FromProdDate.Year).FirstOrDefault(),
Price_OIL = r.Where(b => b.Type == "OIL").Sum(b => b.OwnNetVol == 0 ? 0 : b.UnitPrice * (b.OwnNetVal / b.OwnNetVol)),
Price_GAS = r.Where(b => b.Type == "GAS").Sum(b => b.OwnNetVol == 0 ? 0 : b.UnitPrice * (b.OwnNetVal / b.OwnNetVol)),
Price_NGL = r.Where(b => b.Type == "NGL").Sum(b => b.OwnNetVol == 0 ? 0 : b.UnitPrice * (b.OwnNetVal / b.OwnNetVol))
}).ToList();
我想知道是否有一种方法可以优化这个LINQ
2条答案
按热度按时间wkyowqbh1#
此查询的执行速度应该更快。我把它简化了一点:
8hhllhi22#
必须删除“选择和编辑分组依据”
我的代码(我删除了表的额外两个子查询和过滤器)
我获得了Query with Profiler
我使用Profiler获取您的查询