Skip to content
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

Filtering based on derived property Types in dynamic Linq #519

Closed
chait-01 opened this issue Jun 14, 2021 · 3 comments
Closed

Filtering based on derived property Types in dynamic Linq #519

chait-01 opened this issue Jun 14, 2021 · 3 comments
Labels

Comments

@chait-01
Copy link

chait-01 commented Jun 14, 2021

Here is what to include in your request to make sure we implement a solution as quickly as possible.

1. Description

I am using dynamic lambda expression, and trying to filter records based on a derived type.

var expr1 = DynamicExpressionParser.ParseLambda<Settings, bool>(new ParsingConfig(), true, "c => (c.Applicability As WinApplicability).SKUs.Contains(@0)", result);

Here Appilicability is a property within Settings type, and WinApplicability is a derived type of Applicability. SKUs is a property within WinApplicability.

The above line of code fails with the below error. Any idea how to deal with this issue?

Exception

System.Linq.Dynamic.Core.Exceptions.ParseException: ')' or operator expected

If I try to use static lambda expression, I dont see any error, but result being variable, the code fails with

Expression<Func<Settings, bool>> expr2 = c => (c.Applicability as WinApplicability).SKUs.Contains(result);

since the variable "result" is not pre-computed

"Object reference not set to an instance of an object."
StackTrace = "   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()\r\n   at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()"
@StefH
Copy link
Collaborator

StefH commented Aug 5, 2022

#622

@StefH
Copy link
Collaborator

StefH commented Aug 5, 2022

Hello @chait-01, in your example you use the as as the naming operator (giving a property a different name).

In your example here, you need this code:

var config = new ParsingConfig { ResolveTypesBySimpleName = true };
var expr1 = DynamicExpressionParser.ParseLambda<Settings, bool>(config, true, "As(Applicability, \"WinApplicability\").SKUs.Contains(@0)", result);

See also unit-test:
https://github.com/zzzprojects/System.Linq.Dynamic.Core/blob/master/test/System.Linq.Dynamic.Core.Tests/QueryableTests.Is%2COfType%2CAs%2CCast.cs#L181

Note that I'll also update the documentation on https://dynamic-linq.net/expression-language#is-and-as-examples

@StefH StefH closed this as completed Aug 5, 2022
@StefH
Copy link
Collaborator

StefH commented Aug 6, 2022

@chait-01
Explanation for

in your example you use the as as the naming operator (giving a property a different name).

image

@StefH StefH added the question label Sep 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants