在我的MVC应用程序中,我使用了2个字典来填充DropDownList的SelectList。这些字典将以字符串和日期时间值的形式提供。
对于第一个字典,我有以下代码块,它完全可以正常工作:
if (m_DictDateOrder.Count == 0)
{
m_DictDateOrder = new Dictionary<string, DateTime>();
m_DictDateOrder =
m_OrderManager.ListOrders()
.OrderBy(x => x.m_OrderDate)
.Distinct()
.ToDictionary(x => x.m_OrderDate.ToString(), x => x.m_OrderDate);
}
但是当我到达第二个字典时:
if (m_DictDateShipped.Count == 0)
{
m_DictDateShipped = new Dictionary<string, DateTime>();
m_DictDateShipped =
m_OrderManager.ListOrders()
.OrderBy(x => x.m_ShippedDate)
.Distinct()
.ToDictionary(x => x.m_ShippedDate.ToString(), x => x.m_ShippedDate);
}
我在第二个字典的LINQ请求中遇到了运行时错误:
An item with the same key has already been added.
我最初以为我需要实例化一个新的字典(这就是“new”存在的原因),但不是这样。我做错了什么?
非常感谢!
ToDictionary
可能会再次破坏顺序吗? - cuongle