简体   繁体   English

如何将值os列表对象组合成一个用逗号分隔的字符串

[英]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.

相关问题 如何将包含多个分隔符的字符串合并为用逗号分隔的单个字符串 - How do i combine my string which contain multiple delimeters in to single string seperated by comma 如何在列表数组中将逗号分隔的值分隔为单个Datarow - How to seperate comma seperated values in List Array as a single Datarow 从对象列表中创建逗号分隔的字符串 - Make a comma seperated string from list of objects 从逗号分隔的字符串中按值过滤ObservableCollection - Filter ObservableCollection by value from Comma seperated string 使用AliasToBean转换器在对象属性的映射实体列表到逗号分隔的DTO字符串属性的Nhibernate查询 - Nhibernate Query Over Map Entity List of Object Property To Comma Seperated DTO String Propery Using AliasToBean Transformer 如何比较2个逗号分隔的字符串值并在同一位置的现有列表中更新? - How to compare 2 comma seperated string values and update in existing list at same position? 以逗号分隔的字符串获取ID列表,并与INSERT一起使用 - GET list of Ids in a comma seperated string and use with a INSERT 如何检查单个逗号分隔列中的多个值 - how to check multiple values in a single comma seperated column 将逗号分隔的字符串转换为json - Convert comma seperated string to json 如何匹配逗号分隔的列表并以不同的字符结尾 - How to Match a Comma Seperated List and End with a Different Character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM