Nov*_*ice 1 c# casting asp.net-mvc-2
我有一个这样的课.
public static SkillViewModel GetAllList(string ID)
{
ListViewModel viewModel = new ListViewModel();
DataTable dt = DAL.GetAllList(ID);
List<Skill> skills = new List<Skill>();
foreach (DataRow row in dt.Rows)
{
Skill skill = new Skill() { GroupName = row["GroupName"] != null ? row["GroupName"].ToString() : string.Empty, SubName = row["SubName"] != null ? row["SubName"].ToString() : string.Empty};
skills.Add(skill);
}
viewModel.skills = skills;
viewModel.skills = skills.GroupBy(x => x.GroupName); //Showing error in this line
return viewModel;
}
Run Code Online (Sandbox Code Playgroud)
另一个像这样:
public class SkillViewModel
{
public List<Skill> skills { get; set; }
}
public class Skill
{
[DisplayName("Sub Name")]
[Required]
public string SubName { get; set; }
[DisplayName("Group Name")]
[Required]
public string GroupName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但是说它失败了
错误:无法将类型'System.Collections.Generic.IEnumerable>'隐式转换为'System.Collections.Generic.List'.存在显式转换(您是否错过了演员?)
请帮忙.提前致谢.
只需添加ToList
与SelectMany
在类似语句的结尾:
viewModel.skills = skills.GroupBy(x => x.GroupName).SelectMany(r => r).ToList();
Run Code Online (Sandbox Code Playgroud)
使用SelectMany
会弄平群组,不确定为什么要对数据进行分组,而你的属性却是List<Skill>
归档时间: |
|
查看次数: |
8567 次 |
最近记录: |