c# - 具有相同密钥的项目已存在
问题描述
对 LINQ 相当陌生,无法理解为什么我会收到此错误:
已添加具有相同密钥的项目。
我在想是因为我使用了 3 次 select lambda 表达式?否则,从代码中,我看不到任何属性是如何被多次分配的。有没有更好的方法来写这个和解释?谢谢。
视图模型:
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
共享 ajurisdictionRef
或workareaRef
thenToDictionary()
调用JurisdictionFilter
andWorkareaFilter
将因该异常而失败。
我认为您只想为您的过滤器获取不同的司法管辖区/工作区(这很可能.Distinct()
在ToDictionary
.
推荐阅读
- c# - 内部错误 500 Azure 门户出现错误,但在本地运行正常
- postgresql - PostgreSQL 从值更新将空值视为文本
- r - 如何使for循环忽略几列并在excel文件中执行rbind所有工作表(不管工作表的总量)
- javascript - 如何缩放 javascript 类
- android - 使用函数时的“未定义引用”-Android NDK
- python - Python 密码学:创建由现有 CA 签名的证书,并导出
- swift - 如何在 20%、40%、60%、80% 和 100% 标记处放入带有自定义图像的进度条?
- java - 加载/显示场景后,如何将 CheckBox 添加到 GridPane?
- typescript - 'export * from' 未定义
- r - 如何按小时选择数据