繁体   English   中英

无法将类型 A 转换为类型 B。LINQ to Entities 仅支持转换 EDM 原语或枚举类型

[英]Unable to cast the type A to type B. LINQ to Entities only supports casting EDM primitive or enumeration types

我首先在我的项目中使用 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 to Entities 仅支持转换 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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM