System.InvalidCastException:无法转换'System.Linq.GroupedEnumerable类型的对象

时间:2013-05-28 07:03:39

标签: c# asp.net linq casting

我正在将一些数据分组在一起,以便我可以在首页(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”。

1 个答案:

答案 0 :(得分: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. 按父ID分组questionSubjectGrps
  2. 为每个小组......
  3. ...检索父标题
  4. ...向reportQuestionsGuiDisplayList
  5. 添加新的ReportQuestionsGuiDisplay
  6. 按主题ID订购reportQuestionsGuiDisplayList,然后按父ID订购。
  7. (这可能涉及将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);