Entity Framework Filter “Expression>”

前端 未结 3 1668
余生分开走
余生分开走 2020-11-28 11:02

I\'m trying to create a filter method for Entity framework List and understand better the Expression

I have a Test Function like this.

3条回答
  •  一个人的身影
    2020-11-28 11:16

    Jon and Tim already explained why it doesn't work.

    Assuming that the filter code inside Filter is not trivial, you could change Filter so that it returns an expression EF can translate.

    Let's assume you have this code:

    context.Table.Where(x => x.Name.Length > 500);
    

    You can now create a method the returns this expression:

    Expression> FilterByNameLength(int length)
    {
        return x => x.Name.Length > length;
    }
    

    Usage would be like this:

    context.Table.Where(FilterByNameLength(500));
    

    The expression you build inside FilterByNameLength can be arbitrarily complex as long as you could pass it directly to Where.

提交回复
热议问题