开发者

Filtering derived classes in Entity set by OfType and getting exception

开发者 https://www.devze.com 2023-01-06 17:57 出处:网络
I am trying to filter derived classes in my entities but i am getting exception. var filtered = db.PersonSet.OfType<Person>().

I am trying to filter derived classes in my entities but i am getting exception.

var filtered = db.PersonSet.OfType<Person>().
               Where(pe开发者_运维技巧rson =>person.GetType()==typeof(Person))

I am getting below exception if i try to loop filtered collection by foreach.

"LINQ to Entities does not recognize the method 'System.Type GetType()' method, and this method cannot be translated into a store expression.".

How can i overcome it ? What can be alternative way for filtering ?


Try this:

var persons= db.PersonSet.OfType<Person>().ToList();
var filtered = persons.Where(person =>person.GetType()==typeof(Person))

This should work, but it's unnecessary. The first line should ensure that you only get objects of type Person.


The minute you do the toList, the query is executed and filtering is too late. If you setup an object query then add a Group by clause after your where clause it should perform the secondary filter you want. It is my understanding that the group by clause is for secondary filtering of the results set.

0

精彩评论

暂无评论...
验证码 换一张
取 消