Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将linq to对象查询结果放入类型化词典_C#_Linq - Fatal编程技术网

C# 将linq to对象查询结果放入类型化词典

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

林克还是个新手。似乎应该有一种更优雅的方法来使用ToDictionary来实现这一点,但无法理解。这是我要清理的代码:

 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);