我有一个设计,其中类包含List<> Summary对象,每个Summary是一个半动态属性的字典。也就是说,给定列表中的每个摘要在字典中都具有相同的键。我使用这种设计来构建一组动态的属性"跟踪汇总值,因为根据我的项目规范,这些汇总值将在运行时配置。
问题是:如何将此列表展平,以便列表中的每个项目都被视为 - 如果词典中的键是实际属性?
我已尝试将字典转换为列表的不同变体,但这似乎本质上是错误的,因为我真的需要将键视为属性。我猜我需要使用C#4.0+和ExpandoObject的新动态功能,但我无法正确使用它。
下面的代码显示了基本设置," flattenedSummary"给我我想要的东西 - 动态类型的新列表,其属性是摘要字典的关键字。然而,这是有缺陷的,因为我对属性名称进行了硬编码,但我无法做到这一点,因为直到运行时我才真正了解它们。
flattenedSummary2版本试图压缩列表,但由于返回的类型仍然是List而不是我想要的List,因此不足。
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;
}
答案 0 :(得分:0)
丹尼尔,
我发现这篇文章有一个可能适合你的解决方案。 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;
}
然后从您的主要功能......
List<ExpandoObject> flattenSummary3 = new List<ExpandoObject>();
foreach ( var s in a.Summaries)
{
flattenSummary3.Add(s.ToExpando());
}
现在flattenSummary3变量将包含一个可以通过属性引用的ExpandObjects列表。
我希望这会有所帮助。
答案 1 :(得分:0)
虽然我接受了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元素以用于显示目的。