按日期分组仅部分在实体框架

时间:2015-09-16 06:46:03

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

我一直在尝试对表格数据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.ObjectResult 1 [System.Nullable1 [System.DateTime]] sp_getDate(System.Nullable 1[System.DateTime]) method, and this method cannot be translated into a store expression.

请建议任何解决方法..

1 个答案:

答案 0 :(得分:0)

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