简体   繁体   English

'TruncateTime(System.Nullable`1 [System.DateTime])'不支持对SQL的转换。

[英]TruncateTime(System.Nullable`1[System.DateTime])' has no supported translation to SQL.'

My data class is 我的数据类是

public class Data
{
    public DateTime? Date { get; set; }
    public int Count { get; set; } 
}

And the following query 和以下查询

var queryable = db.MemberStats.Where(ms => ms.MemberId == User.Identity.GetUserId())
                .GroupBy(n => TruncateTime(n.Commited))
                .Select(g => new Data()
                    {
                         Date = g.Key,
                         Count = g.Count()
                    }
                ).ToList();

returns 回报

System.NotSupportedException: 'Method 'System.Nullable`1[System.DateTime] 
TruncateTime(System.Nullable`1[System.DateTime])' has no supported translation to SQL.'

What is wrong with that? 怎么了 How can i fix this one? 我该如何解决这个问题?

update: if i add ToList() before GroupBy() i have 更新:如果我在GroupBy()之前添加ToList()

   at System.Data.Entity.Core.Objects.EntityFunctions.TruncateTime(Nullable`1 dateValue)
   at .Controllers.ChartController.<>c.<Index>b__1_0(MemberStat n) in \Controllers\ChartController.cs:line 30
   at System.Linq.Lookup`2.Create[TSource](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.GroupedEnumerable`3.GetEnumerator()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()

EnityFunctions.TruncateTime and DbFunctions.TruncateTime are Entity Framework (prior EF Core) specific methods which compensate the lack of support of DateTime.Date property when translating LINQ query to SQL. EnityFunctions.TruncateTimeDbFunctions.TruncateTime是特定于实体框架(在EF核心之前)的方法,可弥补在将LINQ查询转换为SQL时缺少对DateTime.Date属性的支持。 Think of them as placeholders inside the query expression tree which are recognized by the corresponding query provider during the query translation. 可以将它们视为查询表达式树内的占位符,这些占位符将在查询转换过程中被相应的查询提供程序识别。 But they are not directly executable (as you already discovered by switching to LINQ to Objects context in your second attempt) nor recognizable (hence not supported) by other query providers. 但是它们不能直接执行(如您在第二次尝试中切换到LINQ to Objects上下文所发现的),也不能被其他查询提供程序识别(因此不支持)。

For LINQ to SQL queries (as it seems to be in your case according to the first exception message), and in general for query providers which support DateTime.Date property (LINQ to Objects, EF Core etc.) you should use that property instead - either directly, or for nullable DateTime properties combined with conditional operator and null check. 对于LINQ to SQL查询(根据您的情况,似乎是第一条异常消息),并且对于支持DateTime.Date属性(LINQ to Objects,EF Core等)的查询提供程序,通常应使用该属性代替-直接或结合条件运算符和null检查的可空DateTime属性。

In your case, either 就您而言,

.GroupBy(n => n.Commited.Date)

or 要么

.GroupBy(n => n.Commited != null ? n.Commited.Value.Date : (DateTime?)null)

can you please try this: 你可以试试这个吗?

var queryable = db.MemberStats.Where(ms => ms.MemberId == User.Identity.GetUserId())
                .GroupBy(n => EntityFunctions.TruncateTime(n.Commited))
                .Select(g => new Data()
                    {
                         Date = g.Key,
                         Count = g.Count()
                    }
                ).ToList();

To use EntityFunctions.TruncateTime you'll need to reference the assembly System.Data.Entity and then include using System.Data.Objects; 要使用EntityFunctions.TruncateTime,您需要引用程序集System.Data.Entity,然后使用System.Data.Objects进行引用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将转储应用到结果时,LINQPad 抛出异常“TruncateTime(System.Nullable`1[System.DateTime])' has no supported translation to SQL” - LINQPad is throwing exception “TruncateTime(System.Nullable`1[System.DateTime])' has no supported translation to SQL” when applying Dump to results [System.DateTime])'没有支持的SQL转换 - [System.DateTime])' has no supported translation to SQL 序列化复杂类型System.Nullable <System.DateTime> - Serialize Complex Type System.Nullable<System.DateTime> “没有为类型‘System.Nullable`1[System.DateTime]’(参数‘property’)定义属性‘System.DateTime Date’”异常 - “Property 'System.DateTime Date' is not defined for type 'System.Nullable`1[System.DateTime]' (Parameter 'property')” exception &#39;System.String&#39; 和 &#39;System.Nullable`1[System.DateTime]&#39; 类型之间没有定义强制运算符 - No coercion operator is defined between types 'System.String' and 'System.Nullable`1[System.DateTime]' EF AsNoTracking导致错误。 类型&#39;System.DateTime&#39;的表达式不能用于赋值类型&#39;System.Nullable`1 [System.DateTime]&#39; - EF AsNoTracking Causes error. Expression of type 'System.DateTime' cannot be used for assignment to type 'System.Nullable`1[System.DateTime]' ExecuteMethodCall:类型&#39;System.Nullable`1 [System.DateTime]&#39;必须声明一个默认(无参数)构造函数 - ExecuteMethodCall: The type 'System.Nullable`1[System.DateTime]' must declare a default (parameterless) constructor LINQ to SQL:“方法”布尔包含(System.String)&#39;没有支持的SQL转换。“ - LINQ to SQL: “Method 'Boolean Contains(System.String)' has no supported translation to SQL.” C# asp.net 核心 webapi 抛出“JSON 值无法转换为 System.Nullable`1[System.DateTime] - C# asp.net core webapi throwing "The JSON value could not be converted to System.Nullable`1[System.DateTime] 不可为空的System.DateTime的值 - Value for Non-Nullable System.DateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM