Dynamic linq query with iqueryable not evaluating properly

.net c# dynamic-linq linq

Question

I am generating a dynamic query based on users selected filters. Then I run this query against a nested object and need to evalute the result, however when I try to query deeper in the nested objects properties it always returns false.

This is my nested object HoldingClass, which has Rows which in turn have attributes i need to evalute.

class HoldingClass
{
    public IEnumerable<Rows> Rows { get; set; }

}

class Rows 
{
    public IEnumerable<Attribute> Attributes { get; set; }
    public string StringProp { get; set; }
}

class Attribute
{
    public string Key { get; set; }
    public object Value { get; set; }
}

I am using an extension class to generate a Queryable from my HoldingClass:

public static IQueryable<HoldingClass> Where(this HoldingClass holdingClass, string predicate, params object[] values)
    => new List<HoldingClass> {holdingClass}.AsQueryable().Where(predicate, values);

I then run a query by inserting a string as "IfCriteria", example query:

var ifCriteria = "Rows.Any() AND Rows.Where(Attributes.Any(Key == "\Filter1\" AND Value == "\ValueToSwitchOn\") AND StringProp == "\PropValue\").Any()"

if (holdingClass.Where(ifCriteria).Any()) {
}

Any ideas what I'm droing wrong? If I Just switch on "StringProp" then it works, but if I go deeper it stops.

1
0
4/20/2020 12:28:12 PM

Popular Answer

Maybe you have a null, and get a null reference exception. Are you sure Attributes is initialized?

If not, you can change .Any() to ?.Any() and deal with null value.

0
4/20/2020 11:10:34 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow