【发布时间】:2017-07-14 02:07:16
【问题描述】:
我想按日期时间列的日期分组(例如,CreateTime ='20017-01-01 01:01:01')以使用 Sum 聚合函数。
- EntityFramework 6 代码优先
- 网络框架 4.5
-
MYSQL 服务器 6.9.9
var myData = from certifiedRecord in DbSet join shopInfo in DbContext.ShopInfos on certifiedRecord.ShopId equals shopInfo.ID into joinedCertify from shopCertifiedRecord in joinedCertify group shopCertifiedRecord by EntityFunctions.Date(certifiedRecord.CreateTime) into g orderby g.Key select new ShopCertifiedDayInfoResponse { Day = g.Key.Value, NormalCount = g.Sum(t => t.CertifiedType.CompareTo(1)), VipCount = g.Sum(t => t.CertifiedType.CompareTo(2)) };
但是没有EntityFunctions.Date 用于Mysql 的EF 函数。
有没有办法解决这个问题?
现在我只是使用 CustomQuery 来解决问题。
我只是想知道如何使用EntityFunctions(可能是另一个瘦子)来解决问题。换句话说,我想使用 Linq for EF 来解决问题。
【问题讨论】:
-
有一个 DateTime.Parse(string) 可以将字符串日期转换为 DateTime 对象。
-
基于this answer,您可以使用DbFunctions.TruncateTime Method (EF 6.0+) 或EntityFunctions.TruncateTime Method (Nullable<DateTime>) 用于早期版本的EF。
-
我想
Group By从 DateTime 列中提取日期(例如 CreateTime),所以我需要使用 'Date' 函数(Mysql 函数)。 -
@KeyurPATEL 它不适合我
-
你尝试了哪一个? (基于您的 EF 版本),以及它给出的错误。如果您发布您尝试过的代码,那就太好了。
标签: c# mysql entity-framework linq