将方法转换为 linq toEntity 表达式以查询实体框架

发布于 2024-11-04 07:20:04 字数 10730 浏览 1 评论 0原文

我有这个问题,我真的不知道如何解决。在解决这个问题之前我问了两个问题,但没有找到适合我情况的正确答案。这里是问题的细节。 我有一个接口和默认实现:

public interface IEntityPriceDefinition
{
    PriceDefinition PriceDefinition { get; }

    bool IsMatch(long additionId);

    bool IsMatch(long? entityId, long? inviterId, long? routeId, long? luggageTypeId);

    bool IsMatch(long? entityId, long? inviterId, long? routeId, long? luggageTypeId,
        long additionId);

    bool IsMatch(long? entityId, Task3 task);

    bool IsMatch(long? entityId, Task3 task, long additionId);
}

public class EntityPriceDefinition : IEntityPriceDefinition
    {
        private PriceDefinition _PriceDefinition;
        private Func<long?, bool> _IsEntityIdMatch;
        private Func<Task3, long?> _TaskValue;

        public PriceDefinition PriceDefinition { get { return _PriceDefinition; } }

        internal EntityPriceDefinition(
            PriceDefinition priceDefinition,
            Func<long?, bool> isEntityIdMatch,
            Func<Task3, long?> taskValue)
        {
            _PriceDefinition = priceDefinition;
            _IsEntityIdMatch = isEntityIdMatch;
            _TaskValue = taskValue;
        }

        public bool IsMatch(long additionId)
        {
            return PriceDefinition.AdditionsPrices.Any(x => x.AdditionId == additionId);
        }

        private bool IsMatch(long? inviterId, long? routeId, long? luggageTypeId)
        {
            bool isMatch = inviterId.HasValue || routeId.HasValue || luggageTypeId.HasValue;
            if (isMatch)
            {
                if (PriceDefinition.InviterId.HasValue && inviterId.HasValue)
                {
                    if (PriceDefinition.InviterId.Value != inviterId.Value) { isMatch = false; }
                }
                if (PriceDefinition.LuggageTypeId.HasValue && luggageTypeId.HasValue)
                {
                    if (PriceDefinition.LuggageTypeId.Value != luggageTypeId.Value) { isMatch = false; }
                }
                if (PriceDefinition.RouteId.HasValue && routeId.HasValue)
                {
                    if (PriceDefinition.RouteId.Value != routeId.Value) { isMatch = false; }
                }
            }
            return isMatch;
        }

        public bool IsMatch(long? entityId, long? inviterId, long? routeId, long? luggageTypeId)
        {
            return _IsEntityIdMatch(entityId) && IsMatch(inviterId, routeId, luggageTypeId);
        }

        public bool IsMatch(long? entityId, long? inviterId, long? routeId, long? luggageTypeId,
            long additionId)
        {
            return IsMatch(entityId, inviterId, routeId, luggageTypeId) && IsMatch(additionId);
        }

        public bool IsMatch(long? entityId, Task3 task)
        {
            bool isMatch = _IsEntityIdMatch(_TaskValue(task)) &&
                IsMatch(task.InviterId, task.RouteId, task.LuggageTypeId);

            for (int i = 0; i < PriceDefinition.Rules.Count && isMatch == true; i++)
            {
                object value = task.GetFieldObjectValue(PriceDefinition.Rules[i].FieldName);
                isMatch = PriceDefinition.Rules[i].IsMatch((value ?? string.Empty).ToString());
            }
            return isMatch;
        }

        public bool IsMatch(long? entityId, Task3 task, long additionId)
        {
            return IsMatch(entityId ,task) && IsMatch(additionId);
        }
    }

我还有 3 个使用默认实现实现 IEntityPriceDefinition 的类。以下是其中两个类(第三个是相同的):

public class CustomerPriceDefinition : IEntityPriceDefinition, IDataEntity
    {
        private IEntityPriceDefinition _EntityPriceDefinition;

        public virtual long PriceDefinitionId { get; set; }
        public virtual long CustomerId { get; set; }
        public virtual PriceDefinition PriceDefinition { get; set; }

        public CustomerPriceDefinition()
        {
            _EntityPriceDefinition = new EntityPriceDefinition(
                PriceDefinition,
                entityId => entityId.HasValue && entityId.Value == CustomerId,
                t => t.CustomerId);
        }

        public bool IsMatch(long additionId)
        {
            return _EntityPriceDefinition.IsMatch(additionId);
        }

        public bool IsMatch(long? customerId, long? inviterId, long? routeId, long? luggageTypeId)
        {
            return _EntityPriceDefinition.IsMatch(customerId, inviterId, routeId, luggageTypeId);
        }

        public bool IsMatch(long? customerId, long? inviterId, long? routeId, long? luggageTypeId,
            long additionId)
        {
            return _EntityPriceDefinition.IsMatch(customerId, inviterId, routeId, luggageTypeId,
                additionId);
        }

        public bool IsMatch(long? customerId, Task3 task)
        {
            return _EntityPriceDefinition.IsMatch(customerId, task);
        }

        public bool IsMatch(long? customerId, Task3 task, long additionId)
        {
            return _EntityPriceDefinition.IsMatch(customerId, task, additionId);
        }
    }

public class WorkerPriceDefinition : IEntityPriceDefinition, IDataEntity
    {
        private IEntityPriceDefinition _EntityPriceDefinition;

        public virtual long PriceDefinitionId { get; set; }
        public virtual long WorkerId { get; set; }
        public virtual PriceDefinition PriceDefinition { get; set; }

        public WorkerPriceDefinition()
        {
            _EntityPriceDefinition = new EntityPriceDefinition(
                PriceDefinition,
                entityId => entityId.HasValue && entityId.Value == WorkerId,
                t => t.WorkerId);
        }

        public bool IsMatch(long additionId)
        {
            return _EntityPriceDefinition.IsMatch(additionId);
        }

        public bool IsMatch(long? workerId, long? inviterId, long? routeId, long? luggageTypeId)
        {
            return _EntityPriceDefinition.IsMatch(workerId, inviterId, routeId, luggageTypeId);
        }

        public bool IsMatch(long? workerId, long? inviterId, long? routeId, long? luggageTypeId,
            long additionId)
        {
            return _EntityPriceDefinition.IsMatch(workerId, inviterId, routeId, luggageTypeId,
                additionId);
        }

        public bool IsMatch(long? workerId, Task3 task)
        {
            return _EntityPriceDefinition.IsMatch(workerId, task);
        }

        public bool IsMatch(long? workerId, Task3 task, long additionId)
        {
            return _EntityPriceDefinition.IsMatch(workerId, task, additionId);
        }
    }

我还有这些类的存储库接口和默认实现:

public interface IEntityPriceDefinitionRepository<T> : IRepository<T>
    where T : class, IEntityPriceDefinition, IDataEntity
{
    IEnumerable<T> GetMatchPrices(
        Guid companyId, bool? isSuggested, bool? isValid,
        long? entityId, long? inviterId, long? routeId, long? luggageTypeId,
        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy,
        Expression<Func<T, object>>[] includes);

    IEnumerable<T> GetMatchPrices(
        Guid companyId, bool? isSuggested, bool? isValid,
        long? entityId, long? inviterId, long? routeId, long? luggageTypeId, long additionId,
        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy,
        Expression<Func<T, object>>[] includes);
}

public class EntityPriceDefinitionRepository<T> : BaseRepository<T>,
    IEntityPriceDefinitionRepository<T> where T : class,IEntityPriceDefinition, IDataEntity
{
    private IEnumerable<T> GetMatchPrices(
        Guid companyId, bool? isSuggested, bool? isValid,
        Expression<Func<T, bool>> isMatch,
        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy,
        Expression<Func<T, object>>[] includes)
    {
        var filters = new Expression<Func<T, bool>>[]{
            x => x.PriceDefinition.CompanyId == companyId,
            x => x.PriceDefinition.IsDeleted == false,
            x => !isValid.HasValue || x.PriceDefinition.IsValid == isValid.Value,
            x => !isSuggested.HasValue || x.PriceDefinition.IsSuggested == isSuggested.Value,
            isMatch
        };

        return GetQuery(filters, orderBy, includes);
    }

    public IEnumerable<T> GetMatchPrices(
        Guid companyId, bool? isSuggested, bool? isValid,
        long? entityId, long? inviterId, long? routeId, long? luggageTypeId,
        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy,
        Expression<Func<T, object>>[] includes)
    {
        return GetMatchPrices(companyId, isSuggested, isValid,
        //////////////////  THIS CAUSE THE EXCEPTION MENTIONED BELOW:  //////////////////
            x => x.IsMatch(entityId, inviterId, routeId, luggageTypeId),
            orderBy, includes);
    }

    public IEnumerable<T> GetMatchPrices(
        Guid companyId, bool? isSuggested, bool? isValid,
        long? entityId, long? inviterId, long? routeId, long? luggageTypeId, long additionId,
        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy,
        Expression<Func<T, object>>[] includes)
    {
        return GetMatchPrices(companyId, isSuggested, isValid,
        //////////////////  THIS CAUSE THE EXCEPTION MENTIONED BELOW:  //////////////////
            x => x.IsMatch(entityId, inviterId, routeId, luggageTypeId, additionId),
            orderBy, includes);
    }        
}

类存储库类只是:

 public class CustomerPriceDefinitionRepository :
        EntityPriceDefinitionRepository<CustomerPriceDefinition> { }

    public class WorkerPriceDefinitionRepository :
        EntityPriceDefinitionRepository<WorkerPriceDefinition> { }

当我调用 CustomerPriceDefinitionRepository 的 GetMatchPrices 方法时,问题发生了。对于上面标记的方法,它总是以异常结束:

LINQ to Entities 无法识别方法 'Boolean IsMatch(System.Nullable1[System.Int64], System.Nullable1[System.Int64], System.Nullable1[ System.Int64], System.Nullable1[System.Int64])' 方法,并且该方法无法转换为存储表达式。

Ladislav Mrnka 在此处回答了我使用模型定义的函数但我希望所有代码都在它的类中而不是在 xml 中。此外,此代码与 edmx 使用的巨大范围无关。此外,我相信为了使用模型定义的函数,我必须定义 3 个方法 - 为 IEntityPriceDefinition 的每个子类定义一个 IsMatch。

我真的不知道如何解决这个问题,也不知道这种情况的最佳解决方案是什么,特别是对于这种不简单的结构。我会感谢任何帮助。

I have this problem and I really don't know how to solve it. I asked two questions before abput this problem but didn't get to figure the right answer for my situation. Here is the problem in details.
I have an interface and default implementation:

public interface IEntityPriceDefinition
{
    PriceDefinition PriceDefinition { get; }

    bool IsMatch(long additionId);

    bool IsMatch(long? entityId, long? inviterId, long? routeId, long? luggageTypeId);

    bool IsMatch(long? entityId, long? inviterId, long? routeId, long? luggageTypeId,
        long additionId);

    bool IsMatch(long? entityId, Task3 task);

    bool IsMatch(long? entityId, Task3 task, long additionId);
}

public class EntityPriceDefinition : IEntityPriceDefinition
    {
        private PriceDefinition _PriceDefinition;
        private Func<long?, bool> _IsEntityIdMatch;
        private Func<Task3, long?> _TaskValue;

        public PriceDefinition PriceDefinition { get { return _PriceDefinition; } }

        internal EntityPriceDefinition(
            PriceDefinition priceDefinition,
            Func<long?, bool> isEntityIdMatch,
            Func<Task3, long?> taskValue)
        {
            _PriceDefinition = priceDefinition;
            _IsEntityIdMatch = isEntityIdMatch;
            _TaskValue = taskValue;
        }

        public bool IsMatch(long additionId)
        {
            return PriceDefinition.AdditionsPrices.Any(x => x.AdditionId == additionId);
        }

        private bool IsMatch(long? inviterId, long? routeId, long? luggageTypeId)
        {
            bool isMatch = inviterId.HasValue || routeId.HasValue || luggageTypeId.HasValue;
            if (isMatch)
            {
                if (PriceDefinition.InviterId.HasValue && inviterId.HasValue)
                {
                    if (PriceDefinition.InviterId.Value != inviterId.Value) { isMatch = false; }
                }
                if (PriceDefinition.LuggageTypeId.HasValue && luggageTypeId.HasValue)
                {
                    if (PriceDefinition.LuggageTypeId.Value != luggageTypeId.Value) { isMatch = false; }
                }
                if (PriceDefinition.RouteId.HasValue && routeId.HasValue)
                {
                    if (PriceDefinition.RouteId.Value != routeId.Value) { isMatch = false; }
                }
            }
            return isMatch;
        }

        public bool IsMatch(long? entityId, long? inviterId, long? routeId, long? luggageTypeId)
        {
            return _IsEntityIdMatch(entityId) && IsMatch(inviterId, routeId, luggageTypeId);
        }

        public bool IsMatch(long? entityId, long? inviterId, long? routeId, long? luggageTypeId,
            long additionId)
        {
            return IsMatch(entityId, inviterId, routeId, luggageTypeId) && IsMatch(additionId);
        }

        public bool IsMatch(long? entityId, Task3 task)
        {
            bool isMatch = _IsEntityIdMatch(_TaskValue(task)) &&
                IsMatch(task.InviterId, task.RouteId, task.LuggageTypeId);

            for (int i = 0; i < PriceDefinition.Rules.Count && isMatch == true; i++)
            {
                object value = task.GetFieldObjectValue(PriceDefinition.Rules[i].FieldName);
                isMatch = PriceDefinition.Rules[i].IsMatch((value ?? string.Empty).ToString());
            }
            return isMatch;
        }

        public bool IsMatch(long? entityId, Task3 task, long additionId)
        {
            return IsMatch(entityId ,task) && IsMatch(additionId);
        }
    }

I also have 3 classes that implement IEntityPriceDefinition using the default implementation. Here are two of those classes (the third is the same):

public class CustomerPriceDefinition : IEntityPriceDefinition, IDataEntity
    {
        private IEntityPriceDefinition _EntityPriceDefinition;

        public virtual long PriceDefinitionId { get; set; }
        public virtual long CustomerId { get; set; }
        public virtual PriceDefinition PriceDefinition { get; set; }

        public CustomerPriceDefinition()
        {
            _EntityPriceDefinition = new EntityPriceDefinition(
                PriceDefinition,
                entityId => entityId.HasValue && entityId.Value == CustomerId,
                t => t.CustomerId);
        }

        public bool IsMatch(long additionId)
        {
            return _EntityPriceDefinition.IsMatch(additionId);
        }

        public bool IsMatch(long? customerId, long? inviterId, long? routeId, long? luggageTypeId)
        {
            return _EntityPriceDefinition.IsMatch(customerId, inviterId, routeId, luggageTypeId);
        }

        public bool IsMatch(long? customerId, long? inviterId, long? routeId, long? luggageTypeId,
            long additionId)
        {
            return _EntityPriceDefinition.IsMatch(customerId, inviterId, routeId, luggageTypeId,
                additionId);
        }

        public bool IsMatch(long? customerId, Task3 task)
        {
            return _EntityPriceDefinition.IsMatch(customerId, task);
        }

        public bool IsMatch(long? customerId, Task3 task, long additionId)
        {
            return _EntityPriceDefinition.IsMatch(customerId, task, additionId);
        }
    }

public class WorkerPriceDefinition : IEntityPriceDefinition, IDataEntity
    {
        private IEntityPriceDefinition _EntityPriceDefinition;

        public virtual long PriceDefinitionId { get; set; }
        public virtual long WorkerId { get; set; }
        public virtual PriceDefinition PriceDefinition { get; set; }

        public WorkerPriceDefinition()
        {
            _EntityPriceDefinition = new EntityPriceDefinition(
                PriceDefinition,
                entityId => entityId.HasValue && entityId.Value == WorkerId,
                t => t.WorkerId);
        }

        public bool IsMatch(long additionId)
        {
            return _EntityPriceDefinition.IsMatch(additionId);
        }

        public bool IsMatch(long? workerId, long? inviterId, long? routeId, long? luggageTypeId)
        {
            return _EntityPriceDefinition.IsMatch(workerId, inviterId, routeId, luggageTypeId);
        }

        public bool IsMatch(long? workerId, long? inviterId, long? routeId, long? luggageTypeId,
            long additionId)
        {
            return _EntityPriceDefinition.IsMatch(workerId, inviterId, routeId, luggageTypeId,
                additionId);
        }

        public bool IsMatch(long? workerId, Task3 task)
        {
            return _EntityPriceDefinition.IsMatch(workerId, task);
        }

        public bool IsMatch(long? workerId, Task3 task, long additionId)
        {
            return _EntityPriceDefinition.IsMatch(workerId, task, additionId);
        }
    }

I have also repository interface and default implementation for those classes:

public interface IEntityPriceDefinitionRepository<T> : IRepository<T>
    where T : class, IEntityPriceDefinition, IDataEntity
{
    IEnumerable<T> GetMatchPrices(
        Guid companyId, bool? isSuggested, bool? isValid,
        long? entityId, long? inviterId, long? routeId, long? luggageTypeId,
        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy,
        Expression<Func<T, object>>[] includes);

    IEnumerable<T> GetMatchPrices(
        Guid companyId, bool? isSuggested, bool? isValid,
        long? entityId, long? inviterId, long? routeId, long? luggageTypeId, long additionId,
        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy,
        Expression<Func<T, object>>[] includes);
}

public class EntityPriceDefinitionRepository<T> : BaseRepository<T>,
    IEntityPriceDefinitionRepository<T> where T : class,IEntityPriceDefinition, IDataEntity
{
    private IEnumerable<T> GetMatchPrices(
        Guid companyId, bool? isSuggested, bool? isValid,
        Expression<Func<T, bool>> isMatch,
        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy,
        Expression<Func<T, object>>[] includes)
    {
        var filters = new Expression<Func<T, bool>>[]{
            x => x.PriceDefinition.CompanyId == companyId,
            x => x.PriceDefinition.IsDeleted == false,
            x => !isValid.HasValue || x.PriceDefinition.IsValid == isValid.Value,
            x => !isSuggested.HasValue || x.PriceDefinition.IsSuggested == isSuggested.Value,
            isMatch
        };

        return GetQuery(filters, orderBy, includes);
    }

    public IEnumerable<T> GetMatchPrices(
        Guid companyId, bool? isSuggested, bool? isValid,
        long? entityId, long? inviterId, long? routeId, long? luggageTypeId,
        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy,
        Expression<Func<T, object>>[] includes)
    {
        return GetMatchPrices(companyId, isSuggested, isValid,
        //////////////////  THIS CAUSE THE EXCEPTION MENTIONED BELOW:  //////////////////
            x => x.IsMatch(entityId, inviterId, routeId, luggageTypeId),
            orderBy, includes);
    }

    public IEnumerable<T> GetMatchPrices(
        Guid companyId, bool? isSuggested, bool? isValid,
        long? entityId, long? inviterId, long? routeId, long? luggageTypeId, long additionId,
        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy,
        Expression<Func<T, object>>[] includes)
    {
        return GetMatchPrices(companyId, isSuggested, isValid,
        //////////////////  THIS CAUSE THE EXCEPTION MENTIONED BELOW:  //////////////////
            x => x.IsMatch(entityId, inviterId, routeId, luggageTypeId, additionId),
            orderBy, includes);
    }        
}

And the classes repository classes are just:

 public class CustomerPriceDefinitionRepository :
        EntityPriceDefinitionRepository<CustomerPriceDefinition> { }

    public class WorkerPriceDefinitionRepository :
        EntityPriceDefinitionRepository<WorkerPriceDefinition> { }

The problem happend when I call CustomerPriceDefinitionRepository's GetMatchPrices method. It always ends up with exception about the method that marked above:

LINQ to Entities does not recognize the method 'Boolean IsMatch(System.Nullable1[System.Int64], System.Nullable1[System.Int64], System.Nullable1[System.Int64], System.Nullable1[System.Int64])' method, and this method cannot be translated into a store expression.

Ladislav Mrnka's answered me here to use model defined functions but I want all my code to be in its classes and not in xml's. In addition, this code doesn't relevant to the huge scope that the edmx uses. In addition, I believe that in order to use model defined functions I will have to define 3 methods - one IsMatch for each sub class of IEntityPriceDefinition.

I really don't know how to solve this problem and what is the best solution for such case especially for such non-simple structure. I will appriciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

噩梦成真你也成魔 2024-11-11 07:20:04

我在 EntityPriceDefinition 中添加了一个静态方法:

   public static Expression<Func<CustomerPriceDefinition, bool>> IsMatchExpression(
        long? entityId, long? inviterId, long? routeId, long? luggageTypeId, long additionId)
    {
        return x =>
            (entityId.HasValue && entityId.Value == x.CustomerId) &&
            (inviterId.HasValue || routeId.HasValue || luggageTypeId.HasValue) &&
            !(
                (x.PriceDefinition.InviterId.HasValue && inviterId.HasValue &&
                    x.PriceDefinition.InviterId.Value != inviterId.Value) ||

                (x.PriceDefinition.LuggageTypeId.HasValue && luggageTypeId.HasValue &&
                    x.PriceDefinition.LuggageTypeId.Value != luggageTypeId.Value) ||

                (x.PriceDefinition.InviterId.HasValue && inviterId.HasValue &&
                    x.PriceDefinition.InviterId.Value != inviterId.Value)
            ) &&
            x.PriceDefinition.AdditionsPrices.Any(a => a.AdditionId == additionId);
    }

这样表达式就知道如何将其转换为查询。

I added a static method in EntityPriceDefinition :

   public static Expression<Func<CustomerPriceDefinition, bool>> IsMatchExpression(
        long? entityId, long? inviterId, long? routeId, long? luggageTypeId, long additionId)
    {
        return x =>
            (entityId.HasValue && entityId.Value == x.CustomerId) &&
            (inviterId.HasValue || routeId.HasValue || luggageTypeId.HasValue) &&
            !(
                (x.PriceDefinition.InviterId.HasValue && inviterId.HasValue &&
                    x.PriceDefinition.InviterId.Value != inviterId.Value) ||

                (x.PriceDefinition.LuggageTypeId.HasValue && luggageTypeId.HasValue &&
                    x.PriceDefinition.LuggageTypeId.Value != luggageTypeId.Value) ||

                (x.PriceDefinition.InviterId.HasValue && inviterId.HasValue &&
                    x.PriceDefinition.InviterId.Value != inviterId.Value)
            ) &&
            x.PriceDefinition.AdditionsPrices.Any(a => a.AdditionId == additionId);
    }

That way the expression knows how to transform it to query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文