-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
Filter properties of a derived class on a list of base class objects #452
Comments
You could cast to
which corresponds to the compiled lambda syntax: (A x) => ((B)x).MyProperty |
unfortunately, this won't out for me, as the provided expression is from an extern (user-generated) source, they should not bother about any casting issues. Aside from that, there is not only a B (there are approx 10 classes, which may be addressed) and only a few of them may have the property. My "workaround" solution could be some kind of a view class, containing a union of properties from those subclasses and convert them into around. But that doesn't sound like a very good architectural approach. |
Do you mean the string is generated somewhere else? How does this external location know that there is a If you are producing the string from other input, then why can't you do the same?
Each property you're trying to use has an associated declared type. If that declared type is not the same as the input type, then in order to read the property you'll have to cast first.
Note that you don't have to create a dedicated class for this; Dynamic LINQ allows you to produce a
and so one. You can create a |
the source of this string is (kind of) a data-grid, containing the union of properties as columns (each row is an object, not available columns are just empty/nulled). The 3rd party knows about those properties by convention (it's basically manual user input - but they should not care about casting & stuff). Data structure is kind of BaseClass > A > B > C for the datagrid I only receive a List. If a user filters a column, only objects containing this property should be taken into consideration. |
Assuming you know, or can determine, that
According to the documentation, this should work: var lst = new List<A>() {
new A(),
new B()
};
var qry = lst.AsQueryable().Where($"np(as(\"{typeof(B).FullName}\").MyProperty) = \"asdf\"");
However, this fails with:
|
Ping @StefanKoenigMUC @StefH @JonathanMagnan -- see previous comment. |
sorry, didnt get an email / didnt see your last comment. Thanks a lot, I'll play around a bit further today! |
@StefanKoenigMUC and @zspitz |
I have a similar requirement, and 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 |
Hi all,
the situation is as follows:
unfortunately and somehow expected, this is not working - as A does obviously not contain "MyProperty" and during expression generation, an exception is thrown
System.Linq.Dynamic.Core.Exceptions.ParseException: 'No property or field 'MyProperty' exists in type 'A''
Is there any way available to build this expression in a way, that subclasses of A are also taken into consideration?
Thanks in advance for your help,
The text was updated successfully, but these errors were encountered: