Filtering derived classes in Entity set to Ofty and exception

I try to filter derived classes in my entities, but I get an exception.

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

I get below exceptions if I try to loop filter through foreach.

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

How can I overcome it? What could be an alternative way to filter?

+3
source share
2 answers

Try the following:

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

This should work, but it is not necessary. The first line should ensure that you get only objects of type Person.

0
source

, toList, , . , Group by where, . , group by .

0

Source: https://habr.com/ru/post/1754312/


All Articles