1

ビューでそれらをループできるように、「ProgramYears」の一意のインスタンスのみを返す必要がある LINQ to Entities クエリがあります。重複しないようにこれを変更する方法がわかりません。

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(),
           };
4

2 に答える 2

2

Distinct演算子を使用できますか?

(from SurveyProgramModel in surveyProgramRepository.Get()
 where SurveyProgramModel.ProgramYear == ProgramYear
 group SurveyProgramModel by SurveyProgramModel.ProgramTypeId)
.Distinct()
.Select(programTypeGroup => 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(),
    };
于 2012-07-06T19:50:16.713 に答える
0

これにより、必要に応じて明確な年が得られます。

var years = surveyProgramRepository.Get()
           .Select(x => x.ProgramYear).Distinct();

または、質問を誤解していて、プロパティのグループごとに異なる年が必要な場合は、メソッドで次の変更を行うことができます。ProgramYearsSelect

...
ProgramYears = programTypeGroup.Select(r => r.ProgramYear).Distinct(),
...

(ただし、コメントに記載されているように、これは意味がありません。これは、 を使用して 1 年を既にフィルター処理しているwhereため、各グループの には 1 年しか含まれないためですProgramYears)

于 2012-07-06T20:53:15.580 に答える