Dynamic LINQ - Groupby with ExpandoObject Results in Error: No Applicable Indexer

c# dynamic dynamic-linq linq

Question

Is it possible to perform a GroupBy using Dynamic LINQ with an ExpandoObject?

Consider the example below; ideally I'd like a grouped list of ExpandoObjects.

Thank you for your time.

var listOfExpandoObjects = new List<ExpandoObject>();
var expandoObject = new ExpandoObject();
((IDictionary<string, object>)expandoObject).Add("DynamicProperty", "RepeatedItem");
((IDictionary<string, object>)expandoObject).Add("OtherProperty", "First");
listOfExpandoObjects.Add(expandoObject);

expandoObject = new ExpandoObject();
((IDictionary<string, object>)expandoObject).Add("DynamicProperty", "RepeatedItem");
((IDictionary<string, object>)expandoObject).Add("OtherProperty", "Second");
listOfExpandoObjects.Add(expandoObject);

var dynamicGroupBy = "new ( it[\"DynamicProperty\"] )";
var groupedItems = listOfExpandoObjects.GroupBy(dynamicGroupBy, "it"); // <------- Error: 'No applicable indexer exists in type 'ExpandoObject''
1
0
5/7/2017 7:01:17 AM

Popular Answer

I think you want something like this

dynamic expandoObject = new ExpandoObject();
expandoObject.DynamicProperty = " RepeatedItem";
expandoObject.OtherProperty = "First";

dynamic secondExpandoObject = new ExpandoObject();
secondExpandoObject.DynamicProperty = "RepeatedItem";
secondExpandoObject.OtherProperty = "Second";

var listOfExpandoObjects = new List<dynamic> { expandoObject, secondExpandoObject };

var dynamicProperty = "DynamicProperty";

var groupedItems =
    from IDictionary<string, object> expando in listOfExpandoObjects
    group expando by expando[dynamicProperty];
0
5/7/2017 7:21:02 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