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)
  {        
  }
}

但我对您的代码似乎实现的目标感到困惑。在我看来,您只是将新 ReportQuestionsGuiDisplay对象添加到平面列表中。为什么首先需要嵌套结构?你不能用一个OrderBy().ThenBy()吗?此外,检索父标题的频率超出了应有的频率。

我建议采用以下方法:

  1. questionSubjectGrps按父 ID分组。
  2. 对于每个组...
  3. ...检索父标题
  4. ... 添加一个新的 ReportQuestionsGuiDisplay 到reportQuestionsGuiDisplayList
  5. 按主题 ID排序reportQuestionsGuiDisplayList,然后按父 ID 排序。

(这可能涉及将 ParentID 和 SubjectID 属性添加到ReportQuestionsGuiDisplay。或者可能是 ReportQuestion,如果合适的话)

或在代码中:

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 回答