2
我在我的项目首先使用EF代码,我有以下实体:无法投类型A至类型B. LINQ到实体仅支持铸造EDM原始或枚举类型
public class WorkcenterCapacity : ITimePeriodEntity
{
public int Id { get; set; }
public decimal AvailableCapacity { get; set; }
public DateTime FromTime { get; set; }
public DateTime ToTime { get; set; }
}
public interface ITimePeriodEntity
{
DateTime FromTime { get; set; }
DateTime ToTime { get; set; }
}
我用PredicateBuilder做一个动态的谓词。我定义以下通用类可用性目的:
public static class CropPredicateBuilder<T> where T : ITimePeriodEntity
{
public static Expression<Func<T, bool>> Creat(DateTime windowStart,
DateTime windowFinish)
{
var result = PredicateBuilder.False<T>();
Expression<Func<T, bool>> completelyInWindowRanges =
x => x.FromTime >= windowStart && x.ToTime <= windowFinish;
Expression<Func<T, bool>> startIsInWindowRanges =
x => x.FromTime >= windowStart && x.FromTime <= windowFinish;
Expression<Func<T, bool>> finishIsInWindowRanges =
x => x.ToTime >= windowStart && x.ToTime <= windowFinish;
Expression<Func<T, bool>> overlapDateRangeWindow =
x => x.FromTime <= windowStart && x.ToTime >= windowFinish;
return result.Or(completelyInWindowRanges)
.Or(startIsInWindowRanges)
.Or(finishIsInWindowRanges)
.Or(overlapDateRangeWindow);
}
}
,并用它如下:
var predicate = CropPredicateBuilder<WorkcenterCapacity>
.Creat(DateTime.Now,DateTime.Now.AddDays(10));
var workcenterCapacities = dbContext.WorkcenterCapacities
.AsNoTracking()
.Where(predicate)
.AsExpandable()
.ToList();
但是当我运行它,我得到以下错误:
Unable to cast the type 'WorkcenterCapacity' to type 'ITimePeriodEntity'. LINQ to Entities only supports casting EDM primitive or enumeration types.
哪有我解决这个问题?