4

I have a design where class holds a List<> of Summary objects, and each Summary is a dictionary of semi-dynamic properties. That is, every Summary in a given list will have the same keys in the dictionary. I'm using this design to build a set of dynamic "properties" to keep track of summary values since, per my project specs, these summary values will be configurable at runtime.

The question is: How can I flatten this List so that each item in the list is treated as-if the keys in the dictionary were actual properties?

I've tried different variations on converting the Dictionary to a list, but that seems inherently wrong since I really need to treat the keys as properties. I'm guessing I need to use the new dynamic feature of C# 4.0+ and the ExpandoObject, but I can't quite get it right.

The code below shows the basic setup, with "flattenedSummary" giving me what I want - a new list of a dynamic type whose properties are the keys of the Summary's dictionary. However, that is flawed in that I've hard-coded the property names and I can't do that since I won't really know them until runtime.

The flattenedSummary2 version attempts to flatten the list but falls short as the returned type is still a List and not the List that I want.

    public class Summary : Dictionary<string, object>
    {
    }

    public class ClassA
    {
        public List<Summary> Summaries = new List<Summary>();
    }

    static void Main(string[] args)
    {
        ClassA a = new ClassA();
        var summary = new Summary();
        summary.Add("Year", 2010);
        summary.Add("Income", 1000m);
        summary.Add("Expenses", 500m);
        a.Summaries.Add(summary);

        summary = new Summary();
        summary.Add("Year", 2011);
        summary.Add("Income", 2000m);
        summary.Add("Expenses", 700m);
        a.Summaries.Add(summary);

        summary = new Summary();
        summary.Add("Year", 2012);
        summary.Add("Income", 1000m);
        summary.Add("Expenses", 800m);
        a.Summaries.Add(summary);

        var flattenedSummary = from s in a.Summaries select new { Year = s["Year"], Income = s["Income"], Expenses = s["Expenses"] };
        ObjectDumper.Write(flattenedSummary, 1);

        var flattenedSummary2 = Convert(a);
        ObjectDumper.Write(flattenedSummary2, 1);

        Console.ReadKey();
    }

    public static List<ExpandoObject> Convert(ClassA a)
    {
        var list = new List<ExpandoObject>();
        foreach (Summary summary in a.Summaries)
        {
            IDictionary<string, object> fields = new ExpandoObject();
            foreach (var field in summary)
            {
                fields.Add(field.Key.ToString(), field.Value);
            }
            dynamic s = fields;
            list.Add(s);
        }

        return list;
    }
4

2 回答 2

1

虽然我接受了 Ed 的回答,因为它非常接近,但我提供了以下代码,以防其他人发现它有用。关键的变化是确保 ExpandoObject 的所有使用都设置为动态的,以便最终的 List 是动态的。如果没有这些更改,检查列表中的类型仍会返回 ExpandoObject(例如,json 序列化给出的是 ExpandoObject 而不是预期的属性名称/值)。

首先,ToExpando() 方法(可能应该称为 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;
}

调用代码如下所示:

    List<dynamic> summaries = new List<dynamic>();
    foreach (var s in a.Summaries)
    {
        summaries.Add(s.DynamicFields.ToExpando());
    }

或者更紧凑的版本:

    a.Summaries.Select(s => s.DynamicFields.ToExpando())

以上所有内容都提供了一个可以引用为的对象:

    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

当然,我的想法是我不知道属性 - 但它们可以序列化为 json,或者通过一些调整,用于绑定网格或其他 UI 元素以用于显示目的。

于 2014-09-25T06:39:12.513 回答
1

丹尼尔,

我发现这篇文章有一个可能对你有用的解决方案。 http://theburningmonk.com/2011/05/idictionarystring-object-to-expandoobject-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;
        }

然后从您的 Main 功能...

List<ExpandoObject> flattenSummary3 = new List<ExpandoObject>();
foreach ( var s in a.Summaries)
{
    flattenSummary3.Add(s.ToExpando());
}

现在 flattenSummary3 变量将包含一个可以通过属性引用的 ExpandObject 列表。

我希望这有帮助。

于 2014-09-25T03:11:52.750 回答