0

フロント ページ (ASPX) に表示できるように、いくつかのデータをグループ化しています。

私は次のコードを持っています:-

IEnumerable<ReportQuestion> questionSubjectGrps =
    questionsBll.GetReportQuestions()
                .Where(x => x.VersionId == iLatestVersion);

reportQuestionsList =
    questionSubjectGrps.GroupBy(q => q.SubjectId,
                                (qid, qs) => qs.GroupBy(q => q.ParentId));

foreach (ReportQuestion reportQuestion in reportQuestionsList)
{
    ReportQuestionsGuiDisplay reportQuestionsGuiDisplay = 
        new ReportQuestionsGuiDisplay();

    reportQuestionsGuiDisplay.ParentQuestionTitle = 
        questionsBll.GetQuestionParents()
                    .FirstOrDefault(
                         x => x.QuestionParentId == reportQuestion.ParentId)
                    .QuestionParentTitle;

    reportQuestionsGuiDisplay.ReportId = reportQuestion.ReportId;

    reportQuestionsGuiDisplayList.Add(reportQuestionsGuiDisplay);
}

フロントエンド (ASPX) には、次のものがあります。

<% foreach (ReportQuestionsGuiDisplay report in reportQuestionsGuiDisplayList)
{%>
    <div class="hrLightBlue"></div>
    <div class="RPTContentTitle2">
      <%= report.SubjectTitle %>
    </div>
    <div class="hrLightBlue"></div>
<%} %>

ただし、次のエラーが発生します。

タイプ ' ' のオブジェクトをタイプ ' System.Linq.GroupedEnumerable``3[SCPerformance.Shared.Models.ReportQuestion,System.Nullable``1[System.Int32],SCPerformance.Shared.Models.ReportQuestion]'にキャストできませんSCPerformance.Shared.Models.ReportQuestion

4

1 に答える 1

1

あなたが呼んでいる:

IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector,
    Func<TKey, IEnumerable<TSource>, TResult> resultSelector)

( http://msdn.microsoft.com/en-us/library/bb549393.aspxを参照)。

あなたの場合、結果はIEnumerable<IGrouping<TKey, TSource>>. ( http://msdn.microsoft.com/en-us/library/bb534501.aspxを参照)

foreach ループで、次のようにします。

foreach (var group in reportQuestionsList)
{
  foreach(ReportQuestion reportQuestion in group)
  {        
  }
}

But I'm confused as to what your code seems to achieve. It looks to me like you're just adding new ReportQuestionsGuiDisplay objects to a flat list. Why do you need a nested structure in the first place? Can't you use a OrderBy().ThenBy()? Also, retrieving the parent title is being done more often than is should.

I would suggest the following approach:

  1. group questionSubjectGrps by parent ID.
  2. for each group...
  3. ... retrieve the parent title
  4. ... add a new ReportQuestionsGuiDisplay to the reportQuestionsGuiDisplayList
  5. order reportQuestionsGuiDisplayList by subject ID, then by parent ID.

(this will probably involve adding ParentID and SubjectID properties to ReportQuestionsGuiDisplay. Or perhaps a ReportQuestion, if that is appropriate)

Or in code:

var questionGroups =
    questionsBll.GetReportQuestions()
                .Where(x => x.VersionId == iLatestVersion)
                .GroupBy(q => q.ParentId);

var displayList = new List<ReportQuestionsGuiDisplay>();

foreach (var questionGroup in questionGroups)
{
    var title = questionsBll.GetQuestionParents()
                    .First(x => x.QuestionParentId == group.Key)
                    .QuestionParentTitle;

    foreach (var question in questionGroup)
    {
        var reportQuestionsGuiDisplay = 
            new ReportQuestionsGuiDisplay() 
            { 
               ParentQuestionTitle = title,
               ReportID = question.ReportID,
               SubjectID = question.SubjectID,
               ParentID = question.ParentID 
            };

        displayList.Add(reportQuestionsGuiDisplay);
    }
}

reportQuestionsGuiDisplayList = displayList.OrderBy(q => q.SubjectID)
                                           .ThenBy(q => q.ParentID);
于 2013-05-28T08:19:13.793 に答える