将groupby datetime与实体框架一起使用会引发异常

nwsw7zdq  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(375)

我使用的是实体框架核心版本3.1.5。
我想把结果按日期分组,比如说按年份。我的最终目标是得到一个组的实体的某些属性的值的总和,但是我甚至没有在没有这个总和的情况下进行分组。我尝试了下面链接中的所有答案,但所有的尝试最终都抛出了一个异常,并显示了一条消息
system.invalidoperationexception:无法翻译linq表达式“dbset.groupby()},keyselector:yyy)”。以可翻译的形式重写查询,或者通过插入对asenumerable()、AsAsAsAsyncEnumerable()、tolist()或ToListSync()的调用显式切换到客户端计算。
代码示例:

var result = await _tenantDbContext.MetricEntities.GroupBy(o => new { o.MetricType, o.CreatedDate.Value.Year }).Select(g => g.Sum(o => o.Value)).ToListAsync();

var result = await _metricRepository.GetQueryable().GroupBy(metric => DbFunctions.TruncateTime(metric.CreatedDate)).OrderBy(group => group.Key).ToListAsync();

var result = await _metricRepository.GetQueryable().GroupBy(metric => DbFunctions.CreateTime(metric.CreatedDate.Value.Year,null,null)).ToListAsync()

var result = _tenantDbContext.MetricEntities
            .GroupBy(x =>
                SqlFunctions.DateAdd("month", SqlFunctions.DateDiff("month", sqlMinDate, x.CreatedDate),
                    sqlMinDate))
            .Select(x => new
            {
                Period = x.Key // DateTime type
            }).ToListAsync();

var result = await _globalDbContext.MetricEntities.GroupBy(x =>
                new
                {
                    Year = x.CreatedDate.Value.Year
                },
            s => new
            {
                InsertCount = s,
            }
        ).Select(g=>new
        {
            InsertCount = g.Count(),
        }).ToListAsync();

链接:
实体框架:按月高效分组
https://atashbahar.com/post/2017-04-27-group-by-day-week-month-quarter-and-year-in-entity-framework
https://entityframework.net/knowledge-base/19225895/linq---grouping-by-date-and-selecting-count
更多链接:
https://www.codeproject.com/questions/1199021/linq-group-by-month-year-and-return-a-sum
在iqueryable中按小时分组
https://www.mikesdotnetting.com/article/257/entity-framework-recipe-grouping-by-year-and-month
ef core“group by无法翻译,将在本地进行评估。”
https://entityframeworkcore.com/knowledge-base/58102821/translating-query-with-group-by-and-count-to-linq
https://entityframeworkcore.com/knowledge-base/43728317/linq-group-by-method-not-generating-the-expected-sql
https://www.mikesdotnetting.com/article/257/entity-framework-recipe-grouping-by-year-and-month

j5fpnvbx

j5fpnvbx1#

仔细看看这个错误,它是关于: linq , GroupBy 它告诉我它不能被“翻译”。
您正在尝试执行sql不支持的操作,很可能是在lambda函数中调用了c函数。所以,基本上是说:要么改变它,要么在记忆中做。
最快的解决方法是在内存中进行,但它会导致大量数据传输。
所以不是:

var restult = db.Table.Where(...).GroupBy(...).ToList();

尝试:

var restult = db.Table.ToList().Where(...).GroupBy(...);
    //or
    var restult = db.Table.Where(...).ToList().GroupBy(...);
gfttwv5a

gfttwv5a2#

唯一适合我的是:

_dbContext.metricEntities.GroupBy(metricEntity =>new
                    {
                        Year = Microsoft.EntityFrameworkCore.EF.Property<DateTime>(metricEntity, "CreatedDate").Date.Year,
                    }
                ).Select(entities =>
                    new MetricGraphNode
                    {
                        X = new DateTime(entities.Key.Year, 1, 1).ToString("yyyy"),
                        Y = entities.Sum(k => k.Value),
                    }).ToList();

当然,您可以添加月、日、小时等。

相关问题