How can you retrieve all records of certain subtype dynamically with Linq to Entities?

Up vote 4 down vote favorite share g+ share fb share tw.

I am trying to get a list of all objects in the database of a specified type. I have done this before when the type was known at compile time, but now I am trying to pass a type into the method and have the method return all the records of that specified type, and I can't get it working. I have tried the following: public IList GetBasicObjectsByProject(int projectId, Type oType) { var results = from o in _objects.

AsQueryable where o.Project. Id == projectId && o.GetType() == oType select o; return results.ToList(); } This didn't work because Linq to Entities doesn't support the GetType() method. Next I tried var results = from o in _objects.

AsQueryable where o.Project. Id == projectId && o is oType select o; This doesn't work because the compiler claims that oType is not of a known type. Using typeof(oType) produces the same error, as does performing OfType() on the IQueryable.

I'm running out of ideas to keep this dynamic without splitting it out into one method per sub-type. Does anyone have any ideas? C# linq entity-framework inheritance linq-to-entities link|improve this question asked Jul 26 '10 at 4:00KallDrexx4,3091342 92% accept rate.

– Igor Zevaka Jul 26 '10 at 6:36 I use where o is WritingObject when mapping to a type manually (where WritingObject is the object type) and that works. And WritingObject is a database table (and a C# class) that has various tables and classes that inherit from it – KallDrexx Jul 26 '10 at 11:39.

Var query = from o in _objects.AsQueryable() where o.Project. Id == projectId select o; var ofType = typeof (Queryable). GetMethod("OfType").

MakeGenericMethod(oType); var list = (IQueryable)ofType. Invoke( null, new object {query}).Cast().ToList(); Note this will include other sub-sub-types too.

Success that works! – KallDrexx Jul 26 '10 at 11:41.

I think the issue here is that Linq-to-Entities is trying to translate o. GetType into SQL, which it obviously can't. After re-reading the question I realised that it's to do with linq to entities not being able to map a CLR type to a database type.

Linq to Entities help talks about that: The LINQ standard query operators that deal with CLR type conversion and testing are supported in the Entity Framework. Only CLR types that map to conceptual model types are supported in LINQ to Entities. For a list of conceptual model types, see Conceptual Model Types.

The above means that it can only convert a handful of supported CLR types into appropriate EDM types as a part of query generation. If the type you are trying to use is not on that list, you will need to filter on type after retrieving the query result. The solution to this problem might be inefficient - you will need to request all records matching projectId criteria and then filter them by type: var results = (from o in _objects.

AsQueryable where o.Project. Id == projectId select o).ToList(); //force query execution here results = from o in results where o.GetType() == oType select o; return results.ToList().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions