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
{{ message }}
Repository navigation
Custom extension not working when using IEnumerable<T> #982
IEnumerable<Person>data=newList<Person>{newPerson{Name="Alice",Age=30},newPerson{Name="Bob",Age=25},newPerson{Name="Charlie",Age=35}};IEnumerable<House>houses=newList<House>{newHouse{Persons=newList<Person>{newPerson{Name="Alice",Age=30},newPerson{Name="Bob",Age=25},newPerson{Name="Charlie",Age=35}}},newHouse{Persons=newList<Person>{newPerson{Name="Dave",Age=40},newPerson{Name="Eve",Age=28}}}};// Configure Dynamic LINQ to use our custom type providervarconfig=newParsingConfig{CustomTypeProvider=newMyCustomTypeProvider(),};stringfilter="a => a.Name.IsEmpty()";System.Linq.Expressions.Expression<Func<House,bool>>expression=System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda<House,bool>(config,false,filter);Func<House,bool>function=expression.Compile();
The filter "a => a.Name.IsEmpty()"; does not compile with error System.Linq.Dynamic.Core.Exceptions.ParseException: 'No applicable method 'IsEmpty' exists in type 'String''
The filter "a => GenericExtensions.IsEmpty(a.Name)"; compiles OK.
The filter "a => GenericExtensions.HasOne(a.Persons)"; compiles OK
The filter "a => GenericExtensions.HasOneGeneric(a.Persons)"; does not compile with error System.Linq.Dynamic.Core.Exceptions.ParseException: 'No applicable method 'HasOneGeneric' exists in type 'GenericExtensions''
Reproduced on current master (0297001, v1.8.0, net10.0) with a DefaultDynamicLinqCustomTypeProvider subclass that adds the extension class to GetCustomTypes():
a => GenericExtensions.IsEmpty(a.Name) and a => GenericExtensions.HasOne(a.Persons) work.
a => GenericExtensions.HasOneGeneric(a.Persons) fails with No applicable method 'HasOneGeneric' exists in type 'GenericExtensions'.
a => a.Persons.HasOne() and a => a.Name.IsEmpty() fail with InvalidOperationException: No generic method '...' on type 'System.Linq.Enumerable' is compatible.... On 1.8.0 I get that exception rather than the ParseException in your report.
Even with a non-enumerable receiver, a => a.Persons.Count.IsEmpty() fails with a ParseException, so this T extensions don't resolve as instance calls at all.
What I found in the code:
DefaultDynamicLinqCustomTypeProvider (around line 124) keys extension methods by the declared type of their first parameter. For this T or this IEnumerable<T> that is an open generic type, so MethodFinder (around line 139) never matches it against a concrete type like string or List<Person>.
For the static call, ExpressionPromoter.Promote (around line 37) only accepts a bare generic parameter (type.IsGenericParameter), not IEnumerable<T>. The MakeGenericMethod step in ExpressionParser (around line 2017) also only infers T for parameters typed exactly T.
For any receiver that implements IEnumerable<> (including string), ParseMemberAccess goes to TryParseEnumerable first (around lines 1987–1993). That builds Expression.Call(typeof(Enumerable), name, ...) and never looks at custom extension methods.
Workaround for now: call the extension statically with a non-generic or concrete signature, e.g. GenericExtensions.HasOne(a.Persons).
I haven't tried a fix yet. Would you take a PR for generic extension support (registering by generic definition plus type inference)?
Activity
menes97 commentedon Nov 19, 2026
Reproduced on current master (0297001, v1.8.0, net10.0) with a
DefaultDynamicLinqCustomTypeProvidersubclass that adds the extension class toGetCustomTypes():a => GenericExtensions.IsEmpty(a.Name)anda => GenericExtensions.HasOne(a.Persons)work.a => GenericExtensions.HasOneGeneric(a.Persons)fails withNo applicable method 'HasOneGeneric' exists in type 'GenericExtensions'.a => a.Persons.HasOne()anda => a.Name.IsEmpty()fail withInvalidOperationException: No generic method '...' on type 'System.Linq.Enumerable' is compatible.... On 1.8.0 I get that exception rather than theParseExceptionin your report.a => a.Persons.Count.IsEmpty()fails with aParseException, sothis Textensions don't resolve as instance calls at all.What I found in the code:
DefaultDynamicLinqCustomTypeProvider(around line 124) keys extension methods by the declared type of their first parameter. Forthis Torthis IEnumerable<T>that is an open generic type, soMethodFinder(around line 139) never matches it against a concrete type likestringorList<Person>.ExpressionPromoter.Promote(around line 37) only accepts a bare generic parameter (type.IsGenericParameter), notIEnumerable<T>. TheMakeGenericMethodstep inExpressionParser(around line 2017) also only infersTfor parameters typed exactlyT.IEnumerable<>(includingstring),ParseMemberAccessgoes toTryParseEnumerablefirst (around lines 1987–1993). That buildsExpression.Call(typeof(Enumerable), name, ...)and never looks at custom extension methods.Workaround for now: call the extension statically with a non-generic or concrete signature, e.g.
GenericExtensions.HasOne(a.Persons).I haven't tried a fix yet. Would you take a PR for generic extension support (registering by generic definition plus type inference)?