简体   繁体   English

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

[英]System.InvalidCastException: Unable to cast object of type 'System.Linq.GroupedEnumerable

I am grouping some data together, so that I can display it on the front page (ASPX). 我将一些数据分组在一起,以便可以在首页(ASPX)上显示它们。

I have the following code :- 我有以下代码:

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

On the front end (ASPX), I have the following : 在前端(ASPX),我具有以下内容:

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

However I am getting the following error : 但是我收到以下错误:

Unable to cast object of type ' System.Linq.GroupedEnumerable``3[SCPerformance.Shared.Models.ReportQuestion,System.Nullable``1[System.Int32],SCPerformance.Shared.Models.ReportQuestion] ' to type ' SCPerformance.Shared.Models.ReportQuestion '. 无法转换类型为' System.Linq.GroupedEnumerable``3[SCPerformance.Shared.Models.ReportQuestion,System.Nullable``1[System.Int32],SCPerformance.Shared.Models.ReportQuestion]的对象来键入' SCPerformance.Shared.Models.ReportQuestion '。

You are calling: 您正在致电:

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

(see http://msdn.microsoft.com/en-us/library/bb549393.aspx ). (请参阅http://msdn.microsoft.com/zh-cn/library/bb549393.aspx )。

In your case, the result is an IEnumerable<IGrouping<TKey, TSource>> . 在您的情况下,结果是IEnumerable<IGrouping<TKey, TSource>> (see http://msdn.microsoft.com/en-us/library/bb534501.aspx ) (请参阅http://msdn.microsoft.com/en-us/library/bb534501.aspx

In your foreach loop, do this: 在您的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. 在我看来,您只是将新的ReportQuestionsGuiDisplay对象添加到平面列表中。 Why do you need a nested structure in the first place? 为什么首先需要嵌套结构? Can't you use a OrderBy().ThenBy() ? 您不能使用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. questionSubjectGrps由父ID。
  2. for each group... 每组...
  3. ... retrieve the parent title ...检索父标题
  4. ... add a new ReportQuestionsGuiDisplay to the reportQuestionsGuiDisplayList ...将新的ReportQuestionsGuiDisplay添加到reportQuestionsGuiDisplayList
  5. order reportQuestionsGuiDisplayList by subject ID, then by parent ID. 按主题ID,然后按父ID排序reportQuestionsGuiDisplayList

(this will probably involve adding ParentID and SubjectID properties to ReportQuestionsGuiDisplay . Or perhaps a ReportQuestion, if that is appropriate) (这可能涉及将ParentID和SubjectID属性添加到ReportQuestionsGuiDisplay 。或者,如果合适的话,也可以添加ReportQuestion)

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法转换类型&#39;System.Linq.GroupedEnumerable的对象类型&#39;System.Collections.Generic.List - Unable to cast object of type 'System.Linq.GroupedEnumerable to type 'System.Collections.Generic.List System.InvalidCastException:无法转换类型的对象 - System.InvalidCastException: Unable to cast object of type System.InvalidCastException:“无法将“System.String”类型的对象转换为“Newtonsoft.Json.Linq.JToken”类型。 - System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'Newtonsoft.Json.Linq.JToken'.' System.InvalidCastException:无法将类型为x的对象强制转换为y - System.InvalidCastException: Unable to cast object of type x to type y System.InvalidCastException: &#39;无法将类型为 &#39;ClassName&#39; 的对象转换为类型 InterfaceName&#39;。 - System.InvalidCastException: 'Unable to cast object of type 'ClassName' to type InterfaceName'.' system.invalidcastexception: &#39;无法转换&#39;system.linq.enumerablequery 类型的对象 - system.invalidcastexception: 'unable to cast object of type 'system.linq.enumerablequery System.InvalidCastException:&#39;无法投射对象 - System.InvalidCastException: 'Unable to cast object LINQ ToDictionary System.InvalidCastException:&#39;无法将类型为&#39;System.Int32&#39;的对象强制转换为类型为&#39;System.String&#39;。 - LINQ ToDictionary System.InvalidCastException: 'Unable to cast object of type 'System.Int32' to type 'System.String'.' System.InvalidCastException:无法将类型为“ System.Object”的对象转换为类型为“ System.IO.StreamWriter” - System.InvalidCastException: Unable to cast object of type 'System.Object' to type 'System.IO.StreamWriter' System.InvalidCastException: &#39;无法将&#39;System.Windows.Forms.TextBox&#39;类型的对象转换为&#39;System.IConvertible&#39; - System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.IConvertible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM