C# 不带TruncateTime()的LINQ中按天分组

C# 不带TruncateTime()的LINQ中按天分组,c#,mysql,asp.net-mvc,linq,razor,C#,Mysql,Asp.net Mvc,Linq,Razor,我对C#Razor MVC项目中的MySQL数据库进行了以下LINQ查询 private Dictionary<DateTime?, int> getOrderQuantityDict(DateTime start, DateTime end, int siteCode) { return (from o in thisDataEntities.this_table where o.created_at >= start &&am

我对C#Razor MVC项目中的MySQL数据库进行了以下LINQ查询

private Dictionary<DateTime?, int> getOrderQuantityDict(DateTime start, DateTime end, int siteCode)
{
    return (from o in thisDataEntities.this_table
        where o.created_at >= start
        && o.created_at <= end
        && o.store_id == siteCode
        select new { OrderDate = o.created_at, Id = o.entity_id})
        .GroupBy(q => q.OrderDate)
        .ToDictionary(q => q.Key, q => q.Count());
}
private Dictionary getOrderQuantityDict(日期时间开始、日期时间结束、int站点代码)
{
返回(来自thisDataEntities.this_表中的o)
其中o.created_在>=开始
&&o.在q.订单日期创建(U)
.ToDictionary(q=>q.Key,q=>q.Count());
}
我需要按天分组。现在q.OrderDate有小时、分钟和秒。我需要在分组时忽略这些


棘手的部分:我需要在不使用
TruncateTime()
的情况下执行此操作。当主机移动数据库时,出于某种原因,我们失去了使用
TruncateTime()
的能力。我们的主持人在这个问题上没有什么帮助,我希望可以找到一个解决办法。

尚未对其进行测试,但以下内容可能会对您有所帮助:

return (from o in thisDataEntities.this_table
    where o.created_at >= start
    && o.created_at <= end
    && o.store_id == siteCode
    select new { OrderDate = o.created_at, Id = o.entity_id})
    .AsEnumerable() //Once this is executed, the database will return the result of the query and any other statement after this will be ran locally so TruncateTime will not be an issue
    .GroupBy(q => q.OrderDate)
    .ToDictionary(q => q.Key, q => q.Count());
return(从thisDataEntities.this_表中的o返回
其中o.created_在>=开始
&&o.在q.订单日期创建(U)
.ToDictionary(q=>q.Key,q=>q.Count());

您可以将日期转换为字符串,并根据日期的字符串表示形式进行分组

return 
    thisDataEntities.this_table
                    .Where(o => o.created_at >= start)
                    .Where(o => o.created_at <= end)
                    .Where(o => o.store_id == siteCode)
                    .Select(o => new
                            {
                                OrderDate = o.created_at, 
                                Id = o.entity_id,
                                OrderDateFormatted = 
                                    SqlFunctions.DateName("yyyy", o.created_at) + "-" +
                                    SqlFunctions.DateName("mm", o.created_at) + "-" +
                                    SqlFunctions.DateName("dd", o.created_at)
                            })
                    .GroupBy(n => n.OrderDateFormatted) // format "2017-10-03"
                    .ToDictionary(g => g.First().OrderDate, g => g.Count());
返回
thisDataEntities.this_表
。其中(o=>o.created\u at>=start)
.Where(o=>o.created\u at o.store\u id==siteCode)
.选择(o=>new
{
OrderDate=o.created_at,
Id=o.entity\u Id,
OrderDateFormatted=
SqlFunctions.DateName(“yyy”,o.created_at)+“-”+
SqlFunctions.DateName(“mm”,o.created_at)+“-”+
SqlFunctions.DateName(“dd”,o.created_at)
})
.GroupBy(n=>n.OrderDateFormatted)//格式“2017-10-03”
.ToDictionary(g=>g.First().OrderDate,g=>g.Count());

使用上述方法,应该在数据库端执行。当然,只有在支持
GroupBy
的情况下才可以。

您可以尝试:
.GroupBy(q=>q.OrderDate.Date)
@JeroenvanLangen-我担心Linq to Entities将无法将属性
.Date
转换为SQL。有一些小细节错误,但是使用
.AsEnumerable()的想法
让我运行C#逻辑是绝对关键的。我将建议一个与我使用的内容相匹配的编辑。非常感谢!