我有一個LINQ to Entities查詢,我只需要返回「ProgramYears」的唯一實例,以便我可以在我的視圖中遍歷它們。我不知道如何改變這一點,以免重複。Group BY - 返回IEnumerable唯一的元素
var data = from SurveyProgramModel in surveyProgramRepository.Get()
where SurveyProgramModel.ProgramYear == ProgramYear
group SurveyProgramModel by SurveyProgramModel.ProgramTypeId into programTypeGroup
select new ProgramTypeViewModel()
{
ProgramTypeIds = programTypeGroup.Key,
ProgramIds = programTypeGroup.Select(r => r.ProgramId),
ProgramYears = programTypeGroup.Select(r => r.ProgramYear),
ProgramTitles = programTypeGroup.Select(r => r.ProgramTitle),
ProgramTypes = programTypeGroup.Select(r => r.ProgramType.ProgramType).Distinct(),
};
只是唯一的實例。例如,2010,2011,2012 – user547794 2012-07-06 19:52:23
您示例中的where子句將可能的年限限制爲1,所以您不能只將包含'ProgramYear'的列表分配給'ProgramYears'嗎? – 2012-07-06 20:31:11