我有一张包含Users的表,每个表可能有很多
LogActivity
,所以存在一对多关系。我想按 DateSearch
的最大属性 LogActivities
对用户集合进行排序(换句话说,具有最早 LogActivities
的用户将位于顶部)。
但是
LogsActivities
的集合可能是空的。另一个要求是用户应该保持IQueryable
,这很重要,因为性能。
无效操作异常:读取数据库值时发生异常。预期类型是“System.DateTime”,但实际值是“System.String”类型”)和警告(“无法翻译 LINQ 表达式“Max()”,将在本地求值。
//(ToListAsync just for testing \\ identities is IQueryable<User>)
//(defaultLogsActivity is LogActivity with DateSearch = DateTime.MinValue)
var qwe = await identities.OrderBy(u => u.LogsUserActivity.DefaultIfEmpty(defaultLogsActivity))
.ToListAsync();// => exception("Expression of type 'UserService.Data.TDO_Models.ManagerUsers.LogsUserActivityEntity' cannot be used for parameter of type 'Microsoft.EntityFrameworkCore.Storage.ValueBuffer' of method DefaultIfEmpty")
var qwe = await identities.OrderBy(u => (u.LogsUserActivity.Count != 0) ? u.LogsUserActivity.Max(l => l.DateSearch) : DateTime.MinValue )
.ToListAsync(); //=> exception ("Invalid operation exception: an exception occurred while reading a database value. The expected type was 'System.DateTime' but the actual value was of type 'System.String'") and warnings ("The LINQ expression '"Max()"' could not be translated and will be evaluated locally.")
public class User
{
public Guid Id { get; set; }
public List<LogActivity> LogActivities { get; set; }
}
public class LogActivity
{
public DateTime DateSearch { get; set; }
}
您似乎在从数据库中转换检索到的数据时遇到问题。
您可以尝试添加到您的模型中
[Timestamp]
或[Column(TypeName = "datetime")]
它应该可以解决你的问题。