我的数据类是
public class Data
{
public DateTime? Date { get; set; }
public int Count { get; set; }
}
以下查询
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();
返回
System.NotSupportedException: 'Method 'System.Nullable`1[System.DateTime]
TruncateTime(System.Nullable`1[System.DateTime])' has no supported translation to SQL.'
这有什么问题?我该如何解决这个问题?
更新 如果我在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()
答案 0 :(得分:1)
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();
要使用EntityFunctions.TruncateTime,您需要引用程序集System.Data.Entity,然后使用System.Data.Objects包含;