无法将类型A转换为类型b.LINQ到实体仅支持转换EDM原语类型或枚举类型

本文关键字:类型 转换 EDM 支持 原语 枚举 实体 LINQ | 更新日期: 2023-09-27 18:11:13

我在我的项目中首先使用EF代码,我有以下实体:

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();

但是当我运行它时,我得到以下错误:

无法将类型"WorkcenterCapacity"转换为类型"ITimePeriodEntity"。实体LINQ只支持转换EDM原语类型或枚举类型。

我该如何解决这个问题?

无法将类型A转换为类型b.LINQ到实体仅支持转换EDM原语类型或枚举类型

试一下

where T : class, ITimePeriodEntity

它希望第一个约束是类。我想这是需要确定的。

顺便说一下,这也可以达到同样的效果,而且可能更快。

public static class CropPredicateBuilder<T> where T : class, ITimePeriodEntity
{
    public static Expression<Func<T, bool>> Creat(DateTime windowStart,
                                                  DateTime windowFinish)
    {
        var result = PredicateBuilder.False<T>();
        Expression<Func<T, bool>> startBeforeToTime =
            x => windowStart <= x.ToTime;
        Expression<Func<T, bool>> finishAfterFromTime =
            x =>  windowFinish >= x.FromTime;
        return result.Or(startBeforeToTime)
            .Or(finishAfterFromTime);
    }
}