简体   繁体   English

具有相同密钥的项目已存在

[英]item with same key already exists

Fairly new to LINQ and cannot understand why I am getting this error: 对LINQ来说还很陌生,不明白为什么我会收到此错误:

An item with the same key has already been added. 具有相同键的项目已被添加。

I was thinking because I am using the select lambda expression 3 times? 我当时在想,因为我要使用选择lambda表达式3次? Otherwise, from the code, I cannot see how any property is being assigned more than once. 否则,从代码中,我将看不到如何多次分配任何属性。 Is there any better way to write this and with explanation? 有什么更好的方法可以编写并解释吗? Thanks. 谢谢。

Viewmodel: 视图模型:

public class AwardWinnersViewModel
{
    public int WinnerId { get; set; }
    public string AwardName { get; set; }
    public DateTime StartDate { get; set; }
    public string Contact { get; set; }
    public int Type { get; set; }
    public int JurisdictionRef { get; set; }
    public int WorkareaRef { get; set; }
    public string Jurisdiction { get; set; }
    public string Workarea { get; set; }
    public string LogoUrl { get; set; }

}

public class AwardWinnersWrapperVM
{
    public IEnumerable<KeyValuePair<short, string>> JurisdictionFilter { get; set; }
    public IEnumerable<KeyValuePair<int, string>> WorkareaFilter { get; set; }
    public IEnumerable<AwardWinnersViewModel> AwardWinners { get; set; }
    public int Page { get; set; }
    public int WinnersPerPage { get; set; }
    public bool HasPrevious { get; set; }
    public bool HasNext { get; set; }
    public int TotalWinners { get; set; } 
    public int? ResultsOutOf { get => (this.WinnersPerPage * (this.Page + 1)) < this.TotalWinners ? (this.WinnersPerPage * (this.Page + 1)) : this.TotalWinners; }
    public int NumberOfPips { get => this.TotalWinners / WinnersPerPage; }
    public int? SelectedJurisdiction { get; set; }
    public int? SelectedWorkarea { get; set; }

}

Controller: 控制器:

[HttpGet]
public async Task<ActionResult> Index(int? page, int? additionalJurisdictionSearch, int? additionalWorkareaSearch)
{
    var awardWinners = await awardWinnersService.GetAwardWinnersAsync();
    var jurisdictions = contentMetadataService.GetJurisdictions();
    var workareas = contentMetadataService.GetWorkareas();
    int pageNumber = page ?? 0;

    var viewModel = new AwardWinnersWrapperVM
    {
        TotalWinners = awardWinners.Count(),
        WinnersPerPage = winnersPerPage,
        Page = pageNumber,
        HasPrevious = pageNumber > 0,
        HasNext = awardWinners.Count() > (winnersPerPage * (pageNumber + 1)),

        AwardWinners = awardWinners.Select(x => new AwardWinnersViewModel
        {
            Type = x.Type,
            Contact = (x.Type == 1)
                ? x.WinnerId != 0 ? contributorService.GetContributor(x.WinnerId, true)?.DisplayName : string.Empty
                : x.WinnerId != 0 ? authorService.GetByRef(x.WinnerId, true)?.AuthorName : string.Empty,
            StartDate = x.StartDate,
            AwardName = x.AwardName,
            Jurisdiction = x.Jurisdiction,
            Workarea = x.Workarea,
            LogoUrl = (x.Type == 1)
                ? contributorService.GetContributor(x.WinnerId, true)?.LogoImageUrlCDN
                : authorService.GetByRef(x.WinnerId, true)?.PhotoUrl,
        }),
        JurisdictionFilter = awardWinners.Select(x => new { id = x.JurisdictionRef, display = (jurisdictions.TryGetValue((short)x.JurisdictionRef, out var jurisdiction) ? jurisdiction.JurisdictionName : string.Empty) }).ToDictionary(key => (short)key.id, val => val.display),
        WorkareaFilter = awardWinners.Select(x => new { id = x.WorkareaRef, display = (workareas.TryGetValue(x.WorkareaRef, out var workarea) ? workarea.WorkareaName : string.Empty) }).ToDictionary(key => key.id, val => val.display)
        .Skip(winnersPerPage * (pageNumber - 1))
        .Take(winnersPerPage)
    };

    return View(viewModel);
}

If any of your awardWinners share a jurisdictionRef or workareaRef then the ToDictionary() calls for JurisdictionFilter and WorkareaFilter will fail with that exception. 如果你的任何awardWinners共享jurisdictionRefworkareaRef那么ToDictionary()呼吁JurisdictionFilterWorkareaFilter将与该异常失败。

I think you want to grab only the distinct jurisdictions/workareas for your filter box (this can likely be achieved using .Distinct() immediately before the ToDictionary ). 我认为您只想为您的过滤器框获取不同的权限/工作区(可以使用.Distinct()之前的ToDictionary .Distinct()来实现)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM