1

我一直在尝试对表格数据进行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.

请提出任何解决方法..

4

1 回答 1

-1

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

于 2015-09-16T07:03:13.723 回答