仅在LINQ中按日期分组到实体框架中的MySql

本文关键字:实体 框架 MySql LINQ 日期 仅在 | 更新日期: 2023-09-27 18:12:51

我一直在尝试将表数据Date分组,而忽略time部分。因为我需要count日期在我的数据源明智的号码。对于这一切,我已经尝试了许多选择,但失败了。

第一次尝试

var data1 = from p in context.qry_view_record
            where p.INT_ITEM_ID == 1                          
            group p by p.DAT_VIEW_START.Date into g
            select new { ViewDate = g.Key, Count = g.Count() };

Fail with Exception The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

第二次尝试

我尝试使用EntityFunctions类将查询修改为

 var data1 = from p in context.qry_view_record
             where p.INT_ITEM_ID == 1                          
             group p by EntityFunctions.TruncateTime(p.DAT_VIEW_START) into g
             select new { ViewDate = g.Key, Count = g.Count() };

Fail with Exception FUNCTION Mynamespace.TruncateTime does not exist

第三次跳

我尝试创建一个User defined MySql Function and stored procedure来截断时间部分,并将过程导入FunctionImport

var data1 = from p in context.qry_view_record
            where p.INT_ITEM_ID == 1                          
            group p by context.sp_getDate(p.DAT_VIEW_START) into g
            select new { ViewDate = g.Key, Count = g.Count() };

Failed with Exception LINQ to Entities does not recognize the method 'System.Data.Objects.ObjectResult 1[System. nullable1]DateTime]] sp_getDate(系统。1[System.DateTime]) method, and this method cannot be translated into a store expression.

请建议任何解决方法…

仅在LINQ中按日期分组到实体框架中的MySql

使用System.Data.Entity.DbFunctions.TruncateTime而不是EntityFunctions.TruncateTime第二次尝试