如何将字典结果绑定到类对象c#linq
Posted
技术标签:
【中文标题】如何将字典结果绑定到类对象c#linq【英文标题】:how to bind dictionary result to class object c# linq 【发布时间】:2021-06-16 15:11:37 【问题描述】: Dictionary<Guid, string> flexEventTriggers = RepositoryContainer.GBM.Flex.Admin_FlexTriggerEvents.AsQueryable()
.Where(x => flexEventTriggerIds.Contains(x.RecordID)).ToDictionary(x => x.RecordID, x => x.Name);
Dictionary<Guid, string> lookupEntities = RepositoryContainer.GBM.Flex.Admin_LookupEntities.AsQueryable()
.Where(x => lookupEnityIds.Contains(x.RecordID)).ToDictionary(x => x.RecordID, x => x.Name);
Dictionary<Guid, string> documents = RepositoryContainer.IDB.Flex.Documents.AsQueryable()
.Where(x => DocumentRecordIds.Contains(x.RecordID)).ToDictionary(x => x.RecordID, x => x.Name);
foreach (var log in flexEventTriggerLogsList)
if (DocumentRecordIds.Count > 0)
foreach (var dcoumentId in DocumentRecordIds)
//Fetch DocumentId's, LookupEntity_RecordID, FlexEventTrigger_RecordID from log additional data
flexEventTriggerLogs.Add(new FlexEventTriggerLog
Name = documents[documentId],
Event_Name = flexEventTriggers[FlexTriggerEventRecordID],
Action_Name = lookupEntities[Entity_RecordID],
);
我将如何在 foreach 循环中为 Name、Event_Name、Action_Name 的对象分配数据。目前它在下面的代码中给出错误。
Name = documents[documentId],
Event_Name = flexEventTriggers[FlexTriggerEventRecordID],
Action_Name = lookupEntities[Entity_RecordID],
【问题讨论】:
那么,错误详情是什么? 【参考方案1】:确保您的DocumentRecordIds
是List<Guid>
的类型,并且属性(Name, Event_Name, Action_Name
) 应该是string
的类型。
你也可以试试这个:
Name = documents.Where(p => p.Key == dcoumentId).Select(p => p.Value).FirstOrDefault();
【讨论】:
以上是关于如何将字典结果绑定到类对象c#linq的主要内容,如果未能解决你的问题,请参考以下文章