score:1
Daniel,
I found this article that has a solution that might work for you. http://theburningmonk.com/2011/05/idictionarystring-object-to-expandoobject-extension-method/
So you would create the extension method...
public static ExpandoObject ToExpando(this IDictionary<string, object> dictionary)
{
var expando = new ExpandoObject();
var expandoDic = (IDictionary<string, object>)expando;
// go through the items in the dictionary and copy over the key value pairs)
foreach (var kvp in dictionary)
{
// if the value can also be turned into an ExpandoObject, then do it!
if (kvp.Value is IDictionary<string, object>)
{
var expandoValue = ((IDictionary<string, object>)kvp.Value).ToExpando();
expandoDic.Add(kvp.Key, expandoValue);
}
else if (kvp.Value is ICollection)
{
// iterate through the collection and convert any strin-object dictionaries
// along the way into expando objects
var itemList = new List<object>();
foreach (var item in (ICollection)kvp.Value)
{
if (item is IDictionary<string, object>)
{
var expandoItem = ((IDictionary<string, object>)item).ToExpando();
itemList.Add(expandoItem);
}
else
{
itemList.Add(item);
}
}
expandoDic.Add(kvp.Key, itemList);
}
else
{
expandoDic.Add(kvp);
}
}
return expando;
}
Then from your Main function...
List<ExpandoObject> flattenSummary3 = new List<ExpandoObject>();
foreach ( var s in a.Summaries)
{
flattenSummary3.Add(s.ToExpando());
}
And now the flattenSummary3 variable will contain a List of ExpandObjects that you can refer by properties.
I hope this helps.
score:1
While I accepted Ed's answer as it was very close, I'm providing the following code in case anyone else finds it useful. The key changes were to ensure that all uses of ExpandoObject were set as dynamic so that the final List was dynamic. Without these changes, examining the types inside the list still returned ExpandoObject (json serializing, for example, gave ExpandoObject instead of the expected property names/values).
First, the ToExpando() method (which should probably be called ToDynamic):
public static dynamic ToExpando(this IDictionary<string, object> dictionary)
{
dynamic expando = new ExpandoObject();
var expandoDic = (IDictionary<string, object>)expando;
// go through the items in the dictionary and copy over the key value pairs)
foreach (var kvp in dictionary)
{
// if the value can also be turned into an ExpandoObject, then do it!
if (kvp.Value is IDictionary<string, object>)
{
var expandoValue = ((IDictionary<string, object>)kvp.Value).ToExpando();
expandoDic.Add(kvp.Key, expandoValue);
}
else if (kvp.Value is ICollection)
{
// iterate through the collection and convert any strin-object dictionaries
// along the way into expando objects
var itemList = new List<object>();
foreach (var item in (ICollection)kvp.Value)
{
if (item is IDictionary<string, object>)
{
var expandoItem = ((IDictionary<string, object>)item).ToExpando();
itemList.Add(expandoItem);
}
else
{
itemList.Add(item);
}
}
expandoDic.Add(kvp.Key, itemList);
}
else
{
expandoDic.Add(kvp);
}
}
return expando;
}
And the calling code would look like this:
List<dynamic> summaries = new List<dynamic>();
foreach (var s in a.Summaries)
{
summaries.Add(s.DynamicFields.ToExpando());
}
Or the even more compact version:
a.Summaries.Select(s => s.DynamicFields.ToExpando())
All of the above provide an object that could be referenced as:
int year = a.Summaries[0].Year; // Year is a dynamic property of type int
decimal income = a.Summaries[0].Income; // Income is a dynamic property of type decimal
Of course, the idea is I won't know the properties - but they can be serialized to json or, with some tweaking, be used to bind a grid or other UI element for display purposes.
Source: stackoverflow.com
Related Articles
- How would I convert List<Dictionary<string, object>> into a List<[new class with dynamic properties]>
- Need to convert a class into another class with distinct value collection
- Convert Object Search Results into Function Class Values
- fill object data source with class in another project
- How can I convert Linq results to DTO class object without iteration
- How to convert a 2-d array into a dictionary object
- convert a flat database resultset into hierarchical object collection in C#
- Linq with where clause in many-to-many EF Code First object
- Combine object properties into a list with LINQ
- LINQ query on object with unknown class
- Converting flattened hierarchical data from SQL Server into a structured JSON object with C#/Linq
- Find object with in class using LINQ
- Entity Framework query to custom object Class with List
- Convert Datatable to Object with Linq and Group by
- Automatically create correct class of object when pulling data with LINQ
- Convert linq query expression with multiple froms into extension method syntax
- Linq: Convert list of object to dictionary, with an object member as value
- C# LINQ with XML, cannot extract multiple fields with same name into object
- Linq result from many tables into an inherit class with nested classes
- How to convert two columns into a single string column with dynamic linq?
- Linq to sql as object data source - designer problem with partial classes
- Convert Linq Expression into the string with given property values
- How to convert dictionary with object to list of that type?
- Convert Entity Object into IEnumerable
- How to group C# class object values. Is this possible with LINQ?
- How to convert SQL select Count with group by clauses into LINQ?
- Convert SQL IN clause with two columns into LINQ
- How to convert this complex SQL Query with Subqueries into LINQ
- Convert this LINQ code back to a Loop (or Why is this object sometimes null)
- convert linq to object query to sql query (no linq to sql code or datacontext)
- ListBoxView populated using Linq based on TimePicker Value
- Concat of two list using LINQ
- LINQ method chaining and granular error handling
- How to compare and sum values of multiple periods using LINQ in VB.Net
- I have a list of date of birth. I want to convert it by age group. i.e: group-1: age 0 - 15, group 2: age 15 -20 etc
- How to order Nested Collections in Linq and EF
- How to handle Only parameterless constructors and initializers are supported in LINQ to Entities
- Linq Select that will generate tied scores for similar values
- How to filter datatable rows in debug mode
- Select columns with specific condition using Linq to SQL
- Improve query generated from entity framework
- Linq error: can't group
- Filter List By items in Array (LINQ)
- Inside of a lamba expression trying to divide two values and compare to another value
- read icollection data using LINQ in C# code
- How to understand this lambda expression?
- How do I exclude a member from Linq-To-Sql mapping?
- LINQ - Accessing a column with the column name as a string parameter
- Linq select in list with 2 many-to-many relationships
- Does this code really cause an "access to modified closure" problem?