-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Expression with value converter could not be translated #17879
Comments
This wouldn't be supported due to existence of value converter. |
@Schaemelhout Issue #10434 is tracking support for allowing value converters to influence generation of SQL--I will add a note to consider this case. However, this particular case could end up being too complicated. This is a case where JSON mapping (#4021) would probably a good fit, once implemented. |
Thanks for the clarification. |
There is a possible workaround to be able to write your query. First you make your labels into a first class object, and stick an explicit string conversion operator to allow the query code to compile. public class PostLabels : List<string>
{
// Add your validation, initialization, overrides, etc..
// Only to support ef core query.
public static explicit operator string(UserExternalIds v)
{
throw new NotImplementedException();
}
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public PostLabels Labels { get; set; }
} Then you would write your query as such. _dbContext.Set<Post>().Where(x => ((string)x.Labels).Contains(".NET CORE")).ToList()
|
Improvement
and your value Conversation should look like this
|
I have created a value converter to store a list of strings as a semicolon-separated string and it looks like EF Core can't translate a LINQ expression to filter these and evaluates it locally.
Is this an example of what's stated in the limitations section of the docs? Or is there another way for me to have EF translate this
WHERE
statement correctly (apart from writing raw SQL)?Steps to reproduce
When executing this query:
_dbContext.Set<Post>().Where(x => x.Labels.Contains(".NET CORE")).ToList()
I get the following warning:
Further technical details
EF Core version: 2.2.6
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 2.2
Operating system: Windows 10
IDE: Visual Studio 2019 16.0
The text was updated successfully, but these errors were encountered: