![](/img/trans.png)
[英]I got an FaultException with "an item with the same key already exists"
[英]item with same key already exists
對LINQ來說還很陌生,不明白為什么我會收到此錯誤:
具有相同鍵的項目已被添加。
我當時在想,因為我要使用選擇lambda表達式3次? 否則,從代碼中,我將看不到如何多次分配任何屬性。 有什么更好的方法可以編寫並解釋嗎? 謝謝。
視圖模型:
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; }
}
控制器:
[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);
}
如果你的任何awardWinners
共享jurisdictionRef
或workareaRef
那么ToDictionary()
呼吁JurisdictionFilter
和WorkareaFilter
將與該異常失敗。
我認為您只想為您的過濾器框獲取不同的權限/工作區(可以使用.Distinct()
之前的ToDictionary
.Distinct()
來實現)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.