Search code examples
.netlinqparsinglambdadynamic

ParseLambda throws exception due to use of enum value after .NET upgrade


I recently upgraded a project from .NET 6 to .NET 8 which has code that makes a call to DynamicExpressionParser.ParseLambda(..):

var e = DynamicExpressionParser.ParseLambda(new[] { q, t, s, d, st }, null, exp);

where exp is a long string which contains the following Linq fragment:

   someCollection.Count(p => p.SomeProperty == MyNamespace.MyEnum.Value1)

After the conversion to .NET 8, the call to ParseLambda(..) began throwing the following exception:

Type 'MyNamespace.MyEnum' not found (at index 504)

I have no idea why this would stop working simply because of the upgrade to .NET 8. Can someone make a recommendation on how to fix this?

I did notice that the version of System.Linq.Dynamic.Core changes from 1.4.5.0 (for .NET 6) to 1.6.0.1 (for .NET 8). So my hunch is that perhaps this is a bug that was introduced in the newer version of the library. Or maybe this is due to some changes made to improve security from injection attacks.

UPDATE #1 <--- I've deleted this update because it turned out to be a red herring.

UPDATE #2: I've found a fix. All I had to do is apply the DynamicLinqType attribute to the enum definition.


Solution

  • I found a fix. All I had to do is apply the DynamicLinqType attribute to the enum definition.