0

I have a dynamic list of objects on which I am using lambda expression where clause to filter items. For example, just consider that it have 3 properties, foo, bar and baz

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

Now if I want to filter item list where foo is false I can use following expression

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

I can even filter the list by strings value as

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

What I want to actually check is if item in list have a specific string value not null of white space. I tried following code

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

It threw an error

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

Though I succeeded to get result by following query

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

I wanted to confirm if there is a way I can use String.IsNullOrWhiteSpace() in this query.

Chaitanya Gadkari
  • 2,669
  • 4
  • 30
  • 54

3 Answers3

0

You can replace "!String.IsNullOrWhiteSpace(baz)" with "!(baz == null || baz.Trim() == string.Empty)" and it should work.

AndyIZ
  • 16
  • 1
  • I just typed this example to present a scenario, Trim is fine, What I am trying to find out is Why it have problem with `String.IsNullOrWhiteSpace(baz)` – Chaitanya Gadkari Jan 13 '17 at 19:52
  • It is a string since if is in double quotes. So can't you just do : string myString = "!String.IsNullOrWhiteSpace(baz)"; – jdweng Jan 13 '17 at 19:54
  • 2
    My understanding is that Linq translates everything into SQL before it executes it. Any function that in not recognized in SQL won't work. – AndyIZ Jan 13 '17 at 20:08
  • @AndyIZ Only if it is Linq-to-Entities or Linq-to-SQL or the likes of them. Linq-to-objects has nothing to do SQL and as far as I understand the author uses dynamic LINQ with plain Linq-to-objects. – Eugene Podskal Jan 13 '17 at 20:30
  • So, it has nothing to do with query on dynamic type but it is related to translation as you guys are discussing. I found this today http://stackoverflow.com/questions/9606979/string-isnullorwhitespace-in-linq-expression , here OP is using normal LINQ query and faced same problem as me. But yes, he is using LINQ to SQL. – Chaitanya Gadkari Jan 14 '17 at 08:44
0

Have a look at System.Linq.Dynamic, there is a great example here.

You will alse need to make sure the List<T> is not List<object>, otherwise System.Linq.Dynamic will not be able to find the properties.

Here is a snippet for your example:

void Main()
{
    var itemList = new List<dynamic>{ new {foo = true, bar = "a", baz = "b" }, new {foo = true, bar = (string)null, baz = "d" } };
    var filtered = itemList.ToAnonymousList().Where("bar != null and bar !=\"\"");
    filtered.Dump();
}

public static class EnumerableEx {
    public static IList ToAnonymousList(this IEnumerable enumerable)
    {
        var enumerator = enumerable.GetEnumerator();
        if (!enumerator.MoveNext())
            throw new Exception("?? No elements??");

        var value = enumerator.Current;
        var returnList = (IList) typeof (List<>)
            .MakeGenericType(value.GetType())
            .GetConstructor(Type.EmptyTypes)
            .Invoke(null);

        returnList.Add(value);

        while (enumerator.MoveNext())
            returnList.Add(enumerator.Current);

        return returnList;
    }
}
Stuart
  • 5,358
  • 19
  • 28
0

I have no problem using your expression - it works fine.

I have used this with objects and entities against an EF storage.

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

So looking at the error, it is stating (moving the order around):

The method IsNullOrWhiteSpace, that returns a Boolean, expects a parameter of type System.String. But what was received was Expression of type System.Func``2[DynamicType,System.Object]'

It appears that you may have referenced an object rather than a string value for your comparison. However, without sample code for the objects you are using, and whether you are using objects, entities, or LinQ to SQL (which I haven't tried) we can only guess at what you have supplied for the expression.

Finally, what is a

'dynamic item list'?

Are you using Dynamic, or is it a normal List<Item>?

Steve Padmore
  • 1,710
  • 1
  • 12
  • 18