C# 将linq to对象查询结果放入类型化词典
林克还是个新手。似乎应该有一种更优雅的方法来使用ToDictionary来实现这一点,但无法理解。这是我要清理的代码:C# 将linq to对象查询结果放入类型化词典,c#,linq,C#,Linq,林克还是个新手。似乎应该有一种更优雅的方法来使用ToDictionary来实现这一点,但无法理解。这是我要清理的代码: var EventOccurrencesMappedToPatientMeds = from pm in allPtMeds from e in thisUsersEventOccurrencesPlusEventData where e.EventContainer.Event.PatientMedId == pm.MedicationId
var EventOccurrencesMappedToPatientMeds = from pm in allPtMeds
from e in thisUsersEventOccurrencesPlusEventData
where e.EventContainer.Event.PatientMedId == pm.MedicationId
select new ScheduleEventOccurrenceMedPair{
PatientMedication = pm,
ScheduledEventOccurrence = e.EventOccurrence
};
int evtOccCount = 1;
foreach (var evtOcc in EventOccurrencesMappedToPatientMeds)
{
// EventOccurrenceChoices is a Dictionary of type <int, IScheduledEventOccurrenceMedPair>
message.EventOccurrenceChoices.Add(evtOccCount,(ScheduleEventOccurrenceMedPair)evtOcc);
}
var EventOccurrencesMappedToPatientMeds=来自所有pTMED中的pm
来自此UserSeventRecurrenceSpluseventData中的e
其中e.EventContainer.Event.PatientMedId==pm.MedicationId
选择新的ScheduleEventOccurrenceMedPair{
PatientMedical=pm,
ScheduledEventOccurrence=e.EventOccurrence
};
int EVTOCCOUNT=1;
foreach(事件发生中的var evtOcc似乎是患者用药)
{
//EventOccurrenceChoices是一个类型为
message.EventOccurrenceChoices.Add(EVTOccount,(ScheduleEventoccurrencedPair)evtOcc);
}
使用ToDictionary
扩展方法
大概是这样的:
var i = 1;
var resultDictionary = (
from pm in allPtMeds
from e in thisUsersEventOccurrencesPlusEventData
where e.EventContainer.Event.PatientMedId == pm.MedicationId
select new ScheduleEventOccurrenceMedPair
{
PatientMedication = pm,
ScheduledEventOccurrence = e.EventOccurrence
})
.ToDictionary(arg => i++, arg => arg);
您可以使用重载包含索引,然后使用
ToDictionary
:
var dict = EventOccurrencesMappedToPatientMeds
.Select((e, i) => new { Event = e, Index = i + 1 })
.ToDictionary(o => o.Index,
o => (ScheduleEventOccurrenceMedPair)o.Event);