Linq with DynamicObject

I have a list where MyType: DynamicObject. The reason for MyType inheriting from DynamicObject is because I need a type that can contain an unknown amount of properties.

Everything works fine until I need to filter the List. Is there a way to make linq that will do something like this:

return all items where any of the properties is empty string or white space?
+5
source share
2 answers

Well, if all the properties of the object are not internally unknown to yourself, you can do it.

, DynamicObject, , , GetDynamicMemberNames(), nuget ImpromptuInterface .

return allItems.Where(x=> Impromptu.GetMemberNames(x, dynamicOnly:true)
                       .Any(y=>String.IsNullOrWhiteSpace(Impromptu.InvokeGet(x,y));

, MyType, , .

return allItems.Where(x => x.MyValues.Any(y=>String.IsNullOrWhitespace(x));
+1

( ) linq ?

, ExpandoObject:

var list = new List<ExpandoObject>();
dynamic e1 = new ExpandoObject();
e1.a = null;
e1.b = "";
dynamic e2 = new ExpandoObject();
e2.x = "xxx";
e2.y = 123;
list.Add(e1);
list.Add(e2);
var res = list.Where(
    item => item.Any(p => p.Value == null || (p.Value is string && string.IsNullOrEmpty((string)p.Value)))
);

ExpandoObject , , , .

+3

All Articles