Lambda/Linq 错误“已添加具有相同键的项目”

发布于 2024-11-05 11:10:04 字数 647 浏览 0 评论 0原文

我收到的错误是“已添加具有相同密钥的项目”。我做错了什么?预先感谢您的帮助。

 using (var Customerconn = new DataClassesDataContext())
        {
            Dictionary<int, string> Empsource = Customerconn.tblOutOfOfficeLogs

                      .Join(Customerconn.tblEmployees,
                      l => l.EmployeeID,
                      emp => emp.EmployeeID,
                      (l, emp) => new { ID = emp.EmployeeID, Name = emp.FirstName + " " + emp.LastName }) 
                .Distinct()
                .ToDictionary(empl => empl.ID, empl => empl.Name);

               //Set combo box source to Empsource


        }

"An item with the same key has already been added" is the error I'm getting. What am I doing wrong? Thank you in advance for any help.

 using (var Customerconn = new DataClassesDataContext())
        {
            Dictionary<int, string> Empsource = Customerconn.tblOutOfOfficeLogs

                      .Join(Customerconn.tblEmployees,
                      l => l.EmployeeID,
                      emp => emp.EmployeeID,
                      (l, emp) => new { ID = emp.EmployeeID, Name = emp.FirstName + " " + emp.LastName }) 
                .Distinct()
                .ToDictionary(empl => empl.ID, empl => empl.Name);

               //Set combo box source to Empsource


        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿! 2024-11-12 11:10:04

即使您确定不存在重复的 EmployeeID,如果同一员工持有多个日志,您也可能会在 join 语句中导致重复。这不是你的问题吗?

以下有效吗?

        Dictionary<int, string> Empsource = tblOutOfOfficeLogs
            .Select(x => x.EmployeeID).Distinct()
                       .Join(tblEmployees,
                             l => l,
                             emp => emp.EmployeeID,
                             (l, emp) => new { ID = emp.EmployeeID, Name = emp.FirstName + " " + emp.LastName })
                       .ToDictionary(empl => empl.ID, empl => empl.Name);

Even though you are sure that no duplicate EmployeeIDs exist you may cause duplicates in the join statement if the same employee holds more than one log. Isn't that your problem?

Does the following work?

        Dictionary<int, string> Empsource = tblOutOfOfficeLogs
            .Select(x => x.EmployeeID).Distinct()
                       .Join(tblEmployees,
                             l => l,
                             emp => emp.EmployeeID,
                             (l, emp) => new { ID = emp.EmployeeID, Name = emp.FirstName + " " + emp.LastName })
                       .ToDictionary(empl => empl.ID, empl => empl.Name);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文