c# - 无法隐式转换类型'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 中。有什么好的解决方案吗?
解决方案
IEnumerable<string>
由于您的Select()
调用 ,您正试图将一组字符串,特别是各种集合分配到一个字符串中。
例如,这一行显然选择了多个简历名称。
resumeName=sg.Select(r=>r.Resumes.Select(m=>m.ResumeName))
如果您不在乎并期望它们具有相同的值,则可以抓住第一个:
resume = item.resumeName.FirstOrDefault()
或者以其他方式展平集合。
也就是说,当您抓取一个集合并尝试将其分配给单个项目时,设计会出现一些问题。
推荐阅读
- android - 如何在不点击 Android 中的继续购物的情况下检测 PayUMoney 响应?
- date - 如何在 JMeter 的 html 报告(“开始时间”部分)中设置当前日期?
- java - 如何在java中解析.eml文件并提取信息?
- qt - 如何从 Qt 中的 stackview 打开的页面打开新页面
- python - Pika - RabbitMQ - 为什么在为消费者使用预取计数 1 时我的交付率大于确认率
- php - 将多个php变量传递给函数
- gstreamer - Gstreamer 到 html 与busybox
- python - 如何从保留点的图像中删除点状噪声?
- javascript - 使用 JavaScript 从 WebSocket 到表的数据
- python - 通过 Django 向应用程序用户发送电子邮件