【问题标题】:Group by Date part only in LINQ to MySql in entity framework仅在实体框架中的 LINQ to MySql 中按日期部分分组
【发布时间】:2015-09-16 06:46:02
【问题描述】:

我一直在尝试明智地将表数据 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() };

失败并出现异常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() };

失败并出现异常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() };

失败并出现异常LINQ to Entities does not recognize the method 'System.Data.Objects.ObjectResult1[System.Nullable1[System.DateTime]] sp_getDate(System.Nullable1[System.DateTime]) method, and this method cannot be translated into a store expression.

请提出任何解决方法..

【问题讨论】:

    标签: c# mysql asp.net entity-framework-5 linq-to-mysql


    【解决方案1】:

    使用 System.Data.Entity.DbFunctions.TruncateTime 代替 EntityFunctions.TruncateTime 在第二次尝试中。

    【讨论】:

    • 我正在使用EF 5.0。所以DbFunction 类不可用。由于EF 6.0 中的Mysql 兼容性问题,请坚持使用EF 5.0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    相关资源
    最近更新 更多