Linq Entity Framework generic filter method

后端 未结 2 475
孤城傲影
孤城傲影 2020-12-19 20:33

I have some methods which perform a standard filter on data from my Entities (using Entity Framework v4).

Example #1:

protected IQueryable

        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-19 21:14

    I wanted to do the same thing with IQueryable as an input parameter type for the generic method.

    I got the runtime exception of "Unable to cast the type.... LINQ to Entities only supports casting Entity Data Model primitive types".

    After some time I recognized, I forgot to add the class constraint for the generic parameter. However IQueryable does not require to have the class constraint, it still needs that to resolve the types at runtime.

    It would have taken me hours, without this post. Thanks :)

提交回复
热议问题