Dynamic lambda expression for where clause

c# dynamic dynamic-linq lambda

Question

I'm filtering things from a dynamic list of objects using a lambda expression where clause. Consider, for instance, that it has three properties: foo, bar, and baz.

class item // let this be current class in dynamic item list
{
    bool foo;
    string bar;
    string baz;
}

Now, I can use the following expression to filter out items in a list when foo is false.

var filtered = itemList.Where("!foo");

I can even use strings values to filter the list.

var filtered = itemList.Where("bar==\"value\""); \\all items with bar = value

I want to determine if a list item's string value is not null or white space and instead has a certain value. I tried the code below

var filtered = itemList.Where("!String.IsNullOrWhiteSpace(baz)");

It made a mistake.

Expression of type 'System.Func`2[DynamicType,System.Object]' cannot be used for parameter of type 'System.String' of method 'Boolean IsNullOrWhiteSpace(System.String)'

Even though I was able to obtain a response from the following query

var filtered = itemList.Where("baz!=null && baz!=\"\"");

I was curious to know if there was a method I could utilizeString.IsNullOrWhiteSpace() in this case.

1
0
1/13/2017 7:38:29 PM

Accepted Answer

You might change "!String. IsNullOrWhiteSpace(baz), and replace "! (baz.Trim() == string.Empty || baz == null) "and it must function.

0
1/13/2017 7:44:41 PM

Popular Answer

I may use your phrase without any issues; it is effective.

I've used this in conjunction with entities and objects in an EF store.

Expression of type 'System.Func`2[DynamicType,System.Object]' cannot be used for parameter of type 'System.String' of method 'Boolean IsNullOrWhiteSpace(System.String)'

Looking at the mistake, it states (by reversing the order):

The approachIsNullOrWhiteSpace , which provides aBoolean , which anticipates a type parameterSystem.String . However, what was given wasExpression of type System.Func``2[DynamicType,System.Object]'

It looks that your comparison may have used an object reference rather than a string value. We can only speculate as to what you have provided for the expression as we don't have any example code for the objects you're using or information about whether you're using entities, objects, or LinQ to SQL (which I haven't tested).

Finally, describe a

'dynamic item list'?

Do you useDynamic or is it a typicalList<Item> ?



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