linq to entities doesn't recognize a method

前端 未结 4 628
感情败类
感情败类 2020-11-27 22:06

I have those methods:

   public int count(
        Guid companyId, Expression> isMatch)
    {
        var filters = new Expression&         


        
4条回答
  •  执念已碎
    2020-11-27 23:08

    Actualy, what you are passing to count look like this function:

    bool anonymous_delagate#123(T entity)
    {
        return entity.IsMatch(a,b,c,d)
    }
    

    But, this would require EF to know, what really method IsMatch, that is called on this entity, means.

    Only thing I can think about recomending now is to use some kind of dynamic expression-forging to create this query dynamicaly. Or rework your design to somethign different.

    Actualy, there is easier and normal method, that requires few steps to acomplish.

    1. Make method IsMatch static.
    2. Return Expression<{your entity here}, bool> directly from IsMatch.
    3. Pass it like : ({your entity here}.IsMatch({parameters}))

    Rest can remain same as you have now.

    Edit: Example This will work with specific entity, so I will asume your entity is Order. Substitute your own entity.

    public static Expression> IsMatch(int id, ...) // static method, that returns filtering expression
    {
         return i => i.Id == id; // create the filtering criteria
    }
    

    Then call it like:

    count(some_guid, Order.IsMatch(entityId, inviterId, routeId, luggageTypeId));
    

提交回复
热议问题