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 Query
- How can you retrieve all records of certain subtype dynamically with Linq to Entities?
- How to write a Linq that can retrieve all parent table records and total of sub-table record, I mean 'separate' into two parts
- How can you handle an IN sub-query with LINQ to SQL?
- How can you do custom sorting in LINQ with null always on the end?
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- How can you update a Linq Expression with additional parameters?
- How can you use LINQ to find Azure AD users with specific licenses using the Azure AD Graph API Client Library 2.0
- How can you use Linq result with RDLC report?
- How can I use Linq with JSON to get all the Ids from this odd JSON?
- How can I add and remove records from a collection with LINQ
- How to sum a field for all records and then return the max of all summed records with Linq
- How to retrieve LINQ object along with all its related info?
- How do you retrieve records and all child records in one database hit in ADO.NET Entities?
- How can you take only the last result with Linq
- How do I go about updating all relative records with my linq instead of first or default?
- How can I dynamically select my Table at runtime with Dynamic LINQ
- How to retrieve data with LINQ to SQL into a weakly-typed object that can be moved around
- How can you handle this sub-query with LINQ to SQL?
- How do you perform a CROSS JOIN with LINQ to SQL?
- How to select only the records with the highest date in LINQ
- How can I reject all changes in a Linq to SQL's DataContext?
- Linq find all with certain type
- How can I do a Union all in Entity Framework LINQ To Entities?
- How do you use LINQ with Sqlite
- How to retrieve last 5 records using LINQ method or query expression in C#
- How I can filter a dataTable with Linq to datatable?
- Can all 'for' loops be replaced with a LINQ statement?
- How can anonymous types be created using LINQ with lambda syntax?
- Can you reverse order a string in one line with LINQ or a LAMBDA expression
- How would you implement LINQ methods with SelectMany?
More Query from same tag
- LINQ from x in Table_name?
- LINQ to SQL Check if DB is Down and Log Why
- setting fields values from multiple joins in linq
- Iterating DataView for Update
- DropDownList value is blank when saving a FormView's filtered result
- Linq Include just one object from a one to many relation
- How to use Linq to query a table dependent on where a user belongs to another table
- C# Filter Listbox From ComboBox Selection Using LinQ
- Exclude linq join condition based on parameter
- Produce List with intent items from row text
- Showing 8 columns together in a cell of a table in Microsoft Report Viewer
- LINQ nested joins
- How to rewrite SQL Union of two tables with Join in LINQ?
- EF ExecuteStoreQuery with unknown type
- How do I group by List<int> in a Dictionary<string, List<int>> where the lists are identical?
- Find index of Regex matches in Linq
- XmlSerializer Convert C# object to xml string
- LINQ join, group by, sum
- How to move a DataTable row to the first position of its DataTable
- LINQ to SQL query returning duplicates
- select query in linq using nhibernate
- How to change sql query to LINQ
- LINQ to SQL(.dbml): how do ExecuteQuery method return DataTable?
- Entity Framework/LINQ Error: The column prefix 'Project1' does not match with a table name or alias name used in the query
- Speeding up SQL to Linq ToList
- foreach iteration after Select
- Querying many-To-many relationship with a list of parameters
- Extract values from XML with linq query
- ASP.Net Gridview not rebind data on button click
- How to do a primary key to equal to an array of int[] in LINQ?