zzzprojects / System.Linq.Dynamic.Core

The .NET Standard / .NET Core version from the System Linq Dynamic functionality.
https://dynamic-linq.net/
Apache License 2.0
1.57k stars 228 forks source link

Like Operator: Unable to load one or more of the requested types. #726

Open Trevortni opened 1 year ago

Trevortni commented 1 year ago

1. Description

When attempting to use the Like operator as such:

string whereRequest = string.Join(" or ", columns.Select(col => $"DynamicFunctions.Like(Convert.ToString({col}).ToLower(), \"%{term.ToLower()}%\"");
var count = await MyObject.Where(new ParsingConfig { ResolveTypesBySimpleName = true }, whereRequest).CountAsync();

I get the following exception

2. Exception

Exception message:
System.Reflection.ReflectionTypeLoadException: 'Unable to load one or more of the requested types.
Could not load type 'Microsoft.EntityFrameworkCore.Metadata.RelationalModelAnnotations' from assembly 'Microsoft.EntityFrameworkCore.Relational, Version=7.0.5.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
Could not load type 'Microsoft.EntityFrameworkCore.Metadata.RelationalPropertyAnnotations' from assembly 'Microsoft.EntityFrameworkCore.Relational, Version=7.0.5.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
Could not load type 'Microsoft.EntityFrameworkCore.Metadata.Internal.RelationalFullAnnotationNames' from assembly 'Microsoft.EntityFrameworkCore.Relational, Version=7.0.5.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.'

Stack trace:
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.Assembly.GetTypes() in /_/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs:line 71
   at System.Linq.Dynamic.Core.CustomTypeProviders.AbstractDynamicLinqCustomTypeProvider.ResolveTypeBySimpleName(IEnumerable`1 assemblies, String simpleTypeName)
   at System.Linq.Dynamic.Core.CustomTypeProviders.DefaultDynamicLinqCustomTypeProvider.ResolveTypeBySimpleName(String simpleTypeName)
   at System.Linq.Dynamic.Core.Parser.TypeFinder.ResolveTypeByUsingCustomTypeProvider(String name, Boolean forceUseCustomTypeProvider)
   at System.Linq.Dynamic.Core.Parser.TypeFinder.FindTypeByName(String name, ParameterExpression[] expressions, Boolean forceUseCustomTypeProvider)
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAsEnum(String id)
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseMemberAccess(Type type, Expression expression)
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseIdentifier()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParsePrimaryStart()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParsePrimary()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseUnary()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseMultiplicative()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAdditive()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseShiftOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseComparisonOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLogicalAndOrOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseIn()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAndOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseOrOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLambdaOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseNullCoalescingOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseConditionalOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseParenExpression()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParsePrimaryStart()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParsePrimary()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseUnary()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseMultiplicative()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAdditive()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseShiftOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseComparisonOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLogicalAndOrOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseIn()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAndOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseOrOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLambdaOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseNullCoalescingOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseConditionalOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.Parse(Type resultType, Boolean createParameterCtor)
   at System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda(Type delegateType, ParsingConfig parsingConfig, Boolean createParameterCtor, ParameterExpression[] parameters, Type resultType, String expression, Object[] values)
   at System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda(ParsingConfig parsingConfig, Boolean createParameterCtor, ParameterExpression[] parameters, Type resultType, String expression, Object[] values)
   at System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda(ParsingConfig parsingConfig, Boolean createParameterCtor, Type itType, Type resultType, String expression, Object[] values)
   at System.Linq.Dynamic.Core.DynamicQueryableExtensions.Where(IQueryable source, ParsingConfig config, String predicate, Object[] args)
   at System.Linq.Dynamic.Core.DynamicQueryableExtensions.Where[TSource](IQueryable`1 source, ParsingConfig config, String predicate, Object[] args)
   at DatabaseContexts.Controllers.DataControllers.ListablesControllerLight`2.<GetSearchListLength>d__18.MoveNext() in C:\Users\lewisw\source\repos\dsd-api\DSD-Web-API\Controllers\DataControllers\ListablesController.cs:line 238

3. Fiddle or Project

Will attempt to further isolate issue tomorrow.

4. Any further technical details

Using Npgsql.EntityFrameworkCore.PostgreSQL 7.0.4 and EntityFrameworkCore 7.0.9

rowanfreeman commented 1 year ago

I don't get that exception but I get a different one.

System.Linq.Dynamic.Core.Exceptions.ParseException: 'No applicable method 'Like' exists in type 'DynamicFunctions"

Screenshot 2023-09-22 104408

The interesting thing is that I'm also using Npgsql. I wonder if the two things are related?

lukasf commented 9 months ago

Has anyone ever got this to work? Being able to use the "LIKE" operator is a very important scenario.

Getting the same error as the previous answer:

System.Linq.Dynamic.Core.Exceptions.ParseException: 'No applicable method 'Like' exists in type 'DynamicFunctions"

When I open the source, I see the Like function is commented out. I tried to implement the class locally, exactly like in DynamicFunctions, but then the expression cannot be translated to SQL (because it passes my local method down to EF, instead of the EF.Functions one).

I also tried to use EF.Functions or DbFunctions.Instance, but that also does not seem to work. It always complains like "enum not found". Dynamic Linq does not seem to support static properties?

Has anyone found a way to do this? CustomTypeProvider or any other solution/hack?

Wasserwecken commented 2 months ago

I figured out a workaround!