我在 ASP.NET MVC4 (Razor View Engine) VS 2012 项目中遇到分组问题。我正在使用实体框架。我的代码如下:
谢谢你的帮助。
我收到错误
Unable to cast object of type 'Grouping[System.String,MyProject.ViewModels.MyViewModel+MyClassData]' to type 'MyClassData'.
我的课
public class MyClassData
{
public int? Id { get; set; }
public int? ParentId { get; set; }
public string Title { get; set; }
}
private readonly List<MyClassData> mData = new List<MyClassData>();
public List<MyClassData> SpecData
{
get { return mData; }
}
IEnumerable<IGrouping<string, MyClassData>>
query = (from t in db.MydbProcedure(temp)
select new MyClassData
{
Id = t.Id,
ParentId = t.ParentId,
Title = t.Title
}).OrderBy(x =>x.Id).GroupBy(y => y.Title);
List<IGrouping<string, MyClassData>> mSpec = query.ToList();
mData .Clear();
mSpec.ForEach(b =>
{
if (b != null)
mData.Add((MyClassData)b);
});