[英]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.TruncateTime
和DbFunctions.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.