【发布时间】:2015-06-05 13:32:49
【问题描述】:
我正在尝试运行以下代码,但是当我检查 SQL 分析器时,它看起来像是在表上运行了一个完整的选择,然后在它从数据库返回所有结果后进行分组。任何帮助表示赞赏。
var result = _dbContext.LogEvent.GroupBy(x => x.EventLevel)
.Select(g => new
{
eventType = g.Key,
total = g.Sum(i => i.Occurrences)
})
.ToList();
【问题讨论】:
-
您可以发布您在分析器中看到的生成的 SQL 吗?
-
它只是执行“从 LogEvent 中选择 column1、column2、column3、column4”。 SQL 中没有分组或求和。
-
我希望看到,如果您执行类似
_dbContext.LogEvent.AsEnumerable()...或_dbContext.LogEvent.ToList()...之类的操作,那么在编写查询时,应该会生成适当的SQL。您可以尝试以下方法:var query = = _dbContext.LogEvent.GroupBy(x => x.EventLevel).Select(g => new { eventType = g.Key, total = g.Sum(i => i.Occurrences) }); Debug.Write(query.ToString());
标签: asp.net-core asp.net-core-mvc entity-framework-core