首页 > 解决方案 > 无法隐式转换类型'System.Collection.Generic.IEnumerable' 到 'string' 同时将数据添加到 ViewModel

问题描述

使用 LinQ 时,可以从外键表数据中检索数据。但是,当我尝试将“添加”到我的 ViewModel 中时,会显示此警告。警告彼此不同。如,

Cannot implicitly convert type 'System.Collection.Generic.IEnumerable<string>' to 'string'

Cannot implicitly convert type 'System.Collection.Generic.IEnumerable<System.Collection.Generic.IEnumerable.IEnumerable<string>' to 'string'.

我尝试转换为 ToString() 但它毫无价值,没有显示错误,但数据已替换为系统消息。我也尝试过 LinQ 加入学生,但我无法以这种方式显示技能逗号分隔。

这是我的代码:

         public ActionResult GetStudentsInfo ()
        {
            var students = (from stud in db.Students
                            group stud by stud.StudentId into sg

                            select new
                            {
                                studentName=sg.Select(s=>s.StudentName).FirstOrDefault(),
                                coutryName=sg.Select(c=>c.Country.CountryName),
                                cityName=sg.Select(ct=>ct.Country.Cities.Select(x=>x.CityName)),
                                skillName=sg.Select(sk=>sk.StudentSkills.Select(s=>s.Skill.SkillName)),
                                resumeName=sg.Select(r=>r.Resumes.Select(m=>m.ResumeName)),
                                dob=sg.Select(d=>d.DateOfBirth)

                            }).ToList();

            List<StudentListVM> studentLists=new List<StudentListVM>();

            foreach (var item in students)
            {
                studentLists.Add(new StudentListVM
                {
                    studentName = item.studentName,
                    country = item.coutryName, //warnings here
                    city = item.cityName, //warnings here
                    skills = string.Join(",", item.skillName),
                    resume = item.resumeName, //warnings here
                    dateOfBirth = item.dob //warnings here
                });
            }
            return View(studentLists);

        }

          ```

StudentListVM class



 public class StudentListVM
    {
        public string studentName { get; set; }
        public string country { get; set; }
        public string city { get; set; }
        public string skills { get; set; }
        public string resume { get; set; }
        public DateTime dateOfBirth { get; set; }
    }

    ```

我以前试过这个


 var students = (from stud in db.Students
                            join con in db.Countries on stud.CountryId equals con.CountryId
                            join ct in db.Cities on stud.CityId equals ct.CityId
                            join rsm in db.Resumes on stud.ResumeID equals rsm.ResumeId
                            join stsk in db.StudentSkills on stud.StudentId equals stsk.StudentId
                            //group stud by stud.StudentId into sg

                            select new StudentListVM()
                            {

                                studentName = stud.StudentName,
                                countries = con.CountryName,
                                cities = ct.CityName,
                                skills=stsk.Skill.SkillName,
                                resumes = rsm.ResumeName,
                                dateOfBirth = stud.DateOfBirth,



                            }).ToList();
        ```


StudentSkill class:

     public partial class StudentSkill
{
    public int StudentSkillsId { get; set; }
    public int StudentId { get; set; }
    public int SkillId { get; set; }

    public virtual Student Student { get; set; }
    public virtual Skill Skill { get; set; }
}

    ```

这将返回除逗号分隔列表中的技能之外的所有内容。我只需要展示我的多项技能,这些技能被多次检查并添加到数据库中的一个单独的表名 StudentSkills 中。有什么好的解决方案吗?

标签: c#linqmodel-view-controller

解决方案


IEnumerable<string>由于您的Select()调用 ,您正试图将一组字符串,特别是各种集合分配到一个字符串中。

例如,这一行显然选择了多个简历名称。

resumeName=sg.Select(r=>r.Resumes.Select(m=>m.ResumeName))

如果您不在乎并期望它们具有相同的值,则可以抓住第一个:

resume = item.resumeName.FirstOrDefault()

或者以其他方式展平集合。

也就是说,当您抓取一个集合并尝试将其分配给单个项目时,设计会出现一些问题。


推荐阅读