Howto use predicates in LINQ to Entities for Entity Framework objects

后端 未结 2 741
难免孤独
难免孤独 2020-12-05 17:37

I\'m using LINQ to Entities for Entity Framework objects in my Data Access Layer.

My goal is to filter as much as I can from the database, without applying filtering

2条回答
  •  眼角桃花
    2020-12-05 18:19

    Methods used in Linq to Entities must be canonically mapped by the Linq provider in order to work. Since the Linq provider, EF in your case, was unable to map your predicate to an internal method, it threw an error.

    For LINQ scenarios, queries against the Entity Framework involve mapping certain CLR methods to methods on the underlying data source through canonical functions. Any method calls in a LINQ to Entities query that are not explicitly mapped to a canonical function will result in a runtime NotSupportedException exception being thrown

    Source: CLR Method to Canonical Function Mapping (http://msdn.microsoft.com/en-us/library/bb738681.aspx)

    You can try to take those methods that ARE mapped and chain them into your Linq expression, or use a stored procedure. But until EF supports all of the CLR, you will be left with having to find a work-around.

    On the plus side, each release seems to add a bit more to the canonical list.

    Worth reading as a possible work-around: http://msdn.microsoft.com/en-us/library/dd456857.aspx

提交回复
热议问题