Accepted answer

Something like: ?

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<WritingObject>().ToList();

Note this will include other sub-sub-types too.


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<WritingObject>();

Related Query

More Query from same tag