1

我有这样的课。

 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;

}

还有一个是这样的:

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

但它通过说失败了

错误:无法将类型“System.Collections.Generic.IEnumerable>”隐式转换为“System.Collections.Generic.List”。存在显式转换(您是否缺少演员表?)

请帮忙。提前致谢。

4

2 回答 2

6

只需在语句末尾添加ToListwith即可:SelectMany

viewModel.skills = skills.GroupBy(x => x.GroupName).SelectMany(r => r).ToList();

usingSelectMany会使组变平,不确定为什么要对数据进行分组,而您的属性是List<Skill>

于 2013-04-15T07:45:54.503 回答
2

您的Skills财产是List<T>.

您需要.ToList()在分配财产时致电。

当你打电话时,GroupBy你将不再拥有,T而是你将拥有IGrouping<T>。这意味着您要么需要过滤您的小组并List<T>退出,要么将您的技能类型更改为:

public List<IGrouping<Skill>> skills { get; set; }
于 2013-04-15T07:45:09.877 回答