列出具有动态数量的元素和动态键名称的<string>

时间:2015-06-28 20:09:12

标签: c# list

我想要一个包含动态密钥名称的列表。

我有以下型号:

public class PlaceTime
{
    public int Id { get; set; }
    public string Place { get; set; }
    public DateTime Day { get; set; }
    public DateTime TimeFrom { get; set; }
    public DateTime TimeTo { get; set; }
    public string Name { get; set; }
}

以下数据:

1, NewYork, 1/1/2015, 8:00 AM, 10:00 AM, Matt
2, NewYork, 1/1/2015, 8:00 AM, 10:00 AM, Ann
3, NewYork, 1/1/2015, 12:00 AM, 14:00 AM, Tom
4, NewYork, 1/1/2015, 15:00 AM, 18:00 AM, Matt

结果我想要以下列表:

List<?> dynamicList;
dynamic[0]: Key = "Place", Value = "New York"
dynamic[0]: Key = "Day", Value = "1/1/2015"
dynamic[0]: Key = "8:00 AM - 10:00 AM", Value = "Matt & Ann"
dynamic[1]: Key = "Place", Value = "New York"
dynamic[1]: Key = "Day", Value = "1/1/2015"
dynamic[1]: Key = "12:00 AM - 14:00 AM", Value = "Tom"
dynamic[2]: Key = "Place", Value = "New York"
dynamic[2]: Key = "Day", Value = "1/1/2015"
dynamic[2]: Key = "15:00 AM - 18:00 AM", Value = "Matt"

如何使用动态数量的元素(取决于存储在PlaceTime模型中的TimeFrom / TimeTo组合)创建具有键名的List,具体取决于模型中的值?

1 个答案:

答案 0 :(得分:0)

这可能是一种方法。使用包含键值对的动态词典的列表。

我不确定它是否是最好的方法,但它完成了工作。

代码图片: http://i.stack.imgur.com/KLkgP.png