-
-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Converting string to another type in both LINQ to Entities and Objects (question) #577
Comments
This is not an issue specific to Dynamic LINQ. I believe the following query would fall on in-memory objects: IQueryable<string> qry = new List<int>().AsQueryable().Cast<string>(); even though the corresponding query would succeed against EF: IQueryable<int> baseQuery = /* populate here */;
IQueryable<string> qry = baseQuery.Cast<string>(); As I understand it, EF handles this using a set of built-in conversions. But in similar scenarios, I would use the in-memory variant as the baseline. I would use a visitor to rewrite the expression tree for EF -- in this case, replacing calls to I would then call This could be in its own extension method off of I wrote sample code for an article that does something similar, rewriting the VB Like operator and its pattern into calls to |
Hello @jbhelm, Did you have time to verify the PR? |
Hi @StefH Apologies for the delay. I did try it and can confirm it does work for LinqToObjects. I will say that for us, this is no longer necessary. Since I originally posted this, we have begun migrating our project from EF6 to EF Core. EF Core LinqToEntities now properly supports |
Thanks for the update. Closing this issue now. |
First, great library, very useful!
We have a scenario where we need the same dynamic LINQ string to execute both against a database using LINQ to Entities, and in-memory objects using LINQ to Objects. Normally this works fine, however, we also occasionally need to convert string values to some other type (integer for example). The problem is the way you do this when using LINQ to Entities is by using
Cast()
, but using LINQ to Objects you must use the appropriateConvert
function, and each call is not supported by the other.For example, take the following dynamic LINQ:
This works as expected for a database because a
Cast
in a database is actually a conversion. Executing this same thing against in-memory objects of course results in the following error because a Cast in .NET is not a conversion, just a cast:For LINQ to Objects, the
Cast()
would need to be replaced by something like:...but that doesn't work with LINQ to Entities because it doesn't support the
Convert
functions.Is there something obvious I'm missing to support parsing/conversions in both scenarios with the same syntax?
I thought I might use the
DynamicLinqTypeAttribute
and make a newIQueryable
extension method likeConvert(type)
that looks at the queryable and determines if it's running against in-memory objects or a database and add the appropriate conversion to the queryable, but it seems extension methods are not supported for LINQ to Entities. (A side question is would it be possible to supportIQueryable
extension methods that also returnIQueryable
for LINQ to Entities?)If there's no built-in way to do this, is there some other way to extend the
IQueryable
functions the library supports? The document mentions "There are several ways to extend the functionality from Dynamic LINQ library" on the "Extending" page, but then only mentions theDynamicLinqTypeAttribute
and no other methods of extending the library.Any help or suggestions are appreciated. Thanks!
The text was updated successfully, but these errors were encountered: