You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the example from the documentation page to generate 'Like' conditions results in an exception: System.Linq.Dynamic.Core.Exceptions.ParseException: "No applicable method 'Like' exists in type 'DynamicFunctions'"
TLDR my Workaround: The string methods .StartsWith, .EndsWith, .Contains work fine and EFCore can translate them to like-conditions.
2. Any further technical details
You might want to add in the documentation that that for using the DynamicFunctions.Like the nuget Microsoft.EntityFrameworkCore.DynamicLinq is required. Without that, the example returns System.Linq.Dynamic.Core.Exceptions.ParseException: "Type 'DynamicFunctions' not found"
Looking into DynamicFunctions.cs, I am not sure why the Like-methods are limited to .Net 2.0 and 2.1? I guess heres the root of the exception because im using .net8 so the methods are not available?
But the previous point does not matter, because when using EF.FunctionsLike directly with a CustomTypeProvider it leads to the next exception: System.Linq.Dynamic.Core.Exceptions.ParseException: "No applicable method 'Like' exists in type 'DbFunctions'".. When you look into DbFunctions.cs there is actually no Like-Method, because it is an extension method in DbFunctionsExtensions.cs
3. Workarounds
I found two workarounds to achive my goal, where my goal:
I dont know when EFCore added support for .StartsWith, .EndsWith, .Contains, but they just work! So there is no need for me to use CustomTypes :)
If someone still needs other EF.Functions, you have to create a custom class that redirects to the EF.Functions like DynamicFunctions.cs and it will wok again.
3. Fiddle or Project
Heres the code with comments to show all the information from above.
// Vanilla approach of generating a Like-condition with EFCore.varcontext=newCustomContext();varexample1=context.Cars.Where(c =>EF.Functions.Like(c.Brand,"%FooBar%"));Console.WriteLine(example1.ToQueryString());Console.WriteLine();// Workaround to make EF.Functions work again.// The 'CustomTypeProvicer' has a Like-method like 'DynamicFunctions'varconfig3=newParsingConfig();config3.ResolveTypesBySimpleName=true;config3.CustomTypeProvider=newCustomTypeProvicer(config3);varexample4=context.Cars.Where(config3,"CustomTypeProvicer.Like(Brand, \"%t%\")");Console.WriteLine(example1.ToQueryString());Console.WriteLine();// Workaround of generating Like-condtions directlyvarexample5=context.Cars.Where("Brand.Contains(\"FooBar\")");Console.WriteLine(example5.ToQueryString());Console.WriteLine();varexample6=context.Cars.Where("Brand.StartsWith(\"FooBar\")");Console.WriteLine(example6.ToQueryString());Console.WriteLine();varexample7=context.Cars.Where("Brand.EndsWith(\"FooBar\")");Console.WriteLine(example7.ToQueryString());Console.WriteLine();// Unable to find the 'Like'-method because im using an unsuported .net version?// Comment this block to go to the next block.varconfig1=newParsingConfig{ResolveTypesBySimpleName=true};varexample2=context.Cars.Where(config1,"DynamicFunctions.Like(Brand, \"%t%\")");Console.WriteLine(example1.ToQueryString());Console.WriteLine();// Unable to find the 'Like'-method because it is an extension method?varconfig2=newParsingConfig();config2.ResolveTypesBySimpleName=true;config2.CustomTypeProvider=newCustomTypeProvicer(config2);varexample3=context.Cars.Where(config2,"EF.Functions.Like(Brand, \"%t%\")");Console.WriteLine(example1.ToQueryString());Console.WriteLine();publicclassCar{publicintId{get;set;}publicstringBrand{get;set;}}publicclassCustomTypeProvicer(ParsingConfigconfig):DefaultDynamicLinqCustomTypeProvider(config){publicoverrideHashSet<Type>GetCustomTypes()=>[typeof(EF),typeof(CustomTypeProvicer)];publicstaticboolLike(string?matchExpression,string?pattern)=>EF.Functions.Like(matchExpression,pattern);}publicclassCustomContext:DbContext{publicDbSet<Car>Cars{get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderbuilder)=>builder.UseSqlite(@"Data Source=.\data.db;");}
The text was updated successfully, but these errors were encountered:
1. Description
Using the example from the documentation page to generate 'Like' conditions results in an exception:
System.Linq.Dynamic.Core.Exceptions.ParseException: "No applicable method 'Like' exists in type 'DynamicFunctions'"
2. Any further technical details
You might want to add in the documentation that that for using the
DynamicFunctions.Like
the nugetMicrosoft.EntityFrameworkCore.DynamicLinq
is required. Without that, the example returnsSystem.Linq.Dynamic.Core.Exceptions.ParseException: "Type 'DynamicFunctions' not found"
Looking into DynamicFunctions.cs, I am not sure why the Like-methods are limited to .Net 2.0 and 2.1? I guess heres the root of the exception because im using .net8 so the methods are not available?
But the previous point does not matter, because when using
EF.FunctionsLike
directly with a CustomTypeProvider it leads to the next exception:System.Linq.Dynamic.Core.Exceptions.ParseException: "No applicable method 'Like' exists in type 'DbFunctions'".
. When you look into DbFunctions.cs there is actually no Like-Method, because it is an extension method in DbFunctionsExtensions.cs3. Workarounds
I found two workarounds to achive my goal, where my goal:
.StartsWith
,.EndsWith
,.Contains
, but they just work! So there is no need for me to use CustomTypes :)3. Fiddle or Project
Heres the code with comments to show all the information from above.
The text was updated successfully, but these errors were encountered: