[英]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: 我建议采用以下方法:
questionSubjectGrps
by parent ID. questionSubjectGrps
由父ID。 reportQuestionsGuiDisplayList
reportQuestionsGuiDisplayList
reportQuestionsGuiDisplayList
by subject ID, then by parent 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.