score:1
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.
score:0
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>();
Source: stackoverflow.com
Related Articles
- How can you retrieve all records of certain subtype dynamically with Linq to Entities?
- Count number of records for a certain condition with linq
- Linq order by with a field to retrieve dynamically in vb.net
- Find a certain value from a source collection in a target collection of different type with linq
- Query a parent-children entities with one-many relationships and retrieve children records based on values in the parent record using LINQ to Entities
- Select multiple records based on list of Id's with linq
- How to select only the records with the highest date in LINQ
- Linq find all with certain type
- How to retrieve last 5 records using LINQ method or query expression in C#
- Find an XElement with a certain attribute name and value with LINQ
- Using LINQ to search a byte array for all subarrays that start/stop with certain byte
- Deleting multiple records with Entity Framework using a single LINQ query
- Dynamically cast IEnumerable to IQueryable or dynamically call AsQueryable with LINQ Expressions
- Dynamically Sorting with LINQ
- Dynamically generate LINQ select with nested properties
- Joining two tables with LINQ while also returning null records from the second table
- How does linq actually execute the code to retrieve data from the data source?
- LINQ Source Code Available
- Linq Dynamically append Where clauses with OR
- Linq with where clause in many-to-many EF Code First object
- how do I write LINQ query to retrieve distinct records based on only specific properties?
- How to group records and retrieve only first group with top N records
- c# - Linq Query to retrieve all objects with a max value
- Get records which do not start with an alphabetical character in linq
- C# Using LINQ Query compare the records with result of process array
- Linq full outer join with NULL records C# from datatables
- LINQ find records with no join
- LINQ In Clause With Group By And TOP N Records Within Each Group
- I am trying create a new list of users from a list of users who have not created an additional object with a certain property using LINQ
- Retrieve and print data from dynamic sql query with linq
- Linq : Combine a list with generic type using left join into a generic type
- Windows Form not getting updates from my database?
- LINQ to SQL how optimise query?
- Linq to sql stored proc not returning results
- Access values from LINQ GroupBy
- Only one expression can be specified in the select list when the subquery is not introduced with EXISTS
- Group By and Count
- xml many to many join using linq
- Database records added to top of table instead of bottom using LINQ
- Dynamic Order By Entity Framework
- LINQ xml finding nodes returns null
- How to nested parsing expression .NET C#
- linq case statement
- LINQ - Is it possible with dynamic LINQ to dynamically specify the from clause?
- Read XML using Linq without element names
- Lookup from CSV
- Linq expression fails in query parser under bizarre conditions
- LINQ query help: searching for data in a Many to Many using Entity Framework
- xPath and max function
- Chain multiple WHERE clauses