I have a method that 'has no translation to SQL' that I want to perform on an IQueryable, is there a way to force the IQueryable to execute without having to store it in some intermediate class?
相关问题
- Check if tie in count of lists using linq
- Trouble with OR in Sharepoint CAML
- get key value pairs from xml using linq
- query and create objects with a one to many relati
- select column from dataset using LINQ
相关文章
- 想问问Linq中有没有根据条件选择要不要关联表。
- Why do I need a ToList() to avoid disposed context
- LINQ .Include() properties from sub-types in TPH i
- Can a method chain be called LINQ?
- Get grouped comma separated values with linq
- How does a LINQ expression know that Where() comes
-
Creating dynamic Expression
> - Entity Framework - An item with the same key has a
and then you can do your linq stuff on that List.
You get that message when you have written a query that LinqToSql doesn't know how to translate into SQL (which is what it says too).
I am not sure I get exactly what you're asking, but as far as I see, you have the following options:
Assuming we rule out #3, let's look at the other 2 examples.
Rewriting it - to help with that, we need your linq query.
Here you take out the part that can't be translated from the initial query, then on your Iqueryable call ToList, and then apply the rest of the query on that list.
And can you execute the query without having to store it? Well, not really, you could always loop through the results and as such not store it in a variable, but obviously the results of the query needs to be stored somewhere.
Is the problem that you want your method to execute locally rather than in the database? If so,
AsEnumerable
is your friend. It's a very simple method, something like:The important thing is that it makes the compile-time type of the result
IEnumerable<T>
rather thanIQueryable<T>
, which means any LINQ query operators you call after that will be the LINQ to Objects ones instead of LINQ to SQL.For example:
You could call
ToList
as suggested elsewhere, but if you're doing filtering and don't really need the full list in memory, callingAsEnumerable
and filtering that result will be more efficient than loading everything first.