我正在遵循这里的建议:
存储值 > 24:00:00 的 .Net 时间跨度的正确 SQL 类型是什么?
在名为 TimesheetEntry 的模型中,我有:
public Int64 NetLengthTicks { get; set; }
[NotMapped]
public TimeSpan NetLength
{
get { return TimeSpan.FromTicks(NetLengthTicks); }
set { NetLengthTicks = value.Ticks; }
}
我正在尝试这个:
var shiftsData = from shift in filteredShifts
where shift.IsDeleted == false
select new
{
shift.TimesheetShiftId,
shift.UserId,
shift.HasShiftEnded,
shift.StartTime,
shift.EndTime,
Entries = from entry in shift.Entries
where entry.IsDeleted == false
select new
{
entry.TimesheetEntryId,
entry.TimesheetShiftId,
entry.EntryType,
entry.StartTimeSpan,
entry.NetLength,
}
};
我得到了例外:
The specified type member 'NetLength' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
我试图将投影更改为:
NetLength = TimeSpan.FromTicks(entry.NetLengthTicks)
但这给出了例外:
LINQ to Entities does not recognize the method 'System.TimeSpan FromTicks(Int64)' method, and this method cannot be translated into a store expression.
然后我尝试创建一个表达式来进行转换:
public static Expression<Func<DAL.Models.TimesheetEntry, TimeSpan>> NetLengthExpression
{
get
{
return e => TimeSpan.FromTicks(e.NetLengthTicks);
}
}
// in the projection
NetLength = NetLengthExpression
但这抛出了:
The LINQ expression node type 'Lambda' is not supported in LINQ to Entities.
有没有办法将 NetLength 公开为要在我的查询中返回的 TimeSpan?