[英]How to combine value os list object in to a single string seperated by comma
I am having list of skills on my Registration page like: 我在“注册”页面上有技能列表,例如:
playing,Dancing,Programming etc . 播放,跳舞,编程等 。
public partial class EmployeeMaster
{
public int Id { get; set; }
public string Fullname { get; set; }
public string Skills { get; set; }
}
Now on my post method i am receiving Skills like this: 现在在我的发布方法中,我将收到如下技能:
This skill is a list object:listSkill object: 该技能是一个列表对象:listSkill对象:
[0]:Name=Playing
Selected=true
[1]:Name=Dancing
Selected=true
[2]:Name=Programming
Selected=false
[HttpPost]
public ActionResult Index(EmployeeModel m)
{
if (ModelState.IsValid)
{
var employeeModel = new EmployeeMaster();
employeeModel.Skills = m.Skills.Select(t => t.Name).SingleOrDefault(); //How to do this as this would take only single value not all skills?
I want to concatinate all this skills in to a single string seperated by comma and store in my database table. 我想将所有这些技巧概括为一个用逗号分隔的字符串,并存储在我的数据库表中。
Simply by using string.Join
with your desired Skills
property ( Name
in this case): 只需使用
string.Join
和所需的Skills
属性(在这种情况下为Name
)即可:
employeeModel.Skills = string.Join(", ", m.Skills.Select(t => t.Name));
This will concatenate all your skill names using the ", "
separator. 这将使用
", "
分隔符连接您的所有技能名称。
employeeModel.Skills = string.Join(",", m.Skills.Select(t => t.Name).Distinct().ToList());
这样的建议对您很有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.