【发布时间】:2013-02-16 05:17:48
【问题描述】:
在我的 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.
虽然我首先添加以实例化一个新字典(这就是“新”存在的原因),但不是。我做错了什么?
非常感谢!
【问题讨论】:
标签: c# asp.net-mvc dictionary