gpt4 book ai didi

c# - 如何将 value os list 对象组合成一个用逗号分隔的字符串

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:53 25 4
gpt4 key购买 nike

我的注册页面上有技能列表,例如:

演奏、跳舞、编程等

 public partial class EmployeeMaster
{
public int Id { get; set; }
public string Fullname { get; set; }
public string Skills { get; set; }
}

现在,在我的发帖方法中,我收到了这样的技能:

这个技能是一个列表对象: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?

我想将所有这些技能连接到一个由逗号分隔的字符串中,并存储在我的数据库表中。

最佳答案

只需将 string.Join 与您想要的 Skills 属性(在本例中为 Name)一起使用即可:

employeeModel.Skills = string.Join(", ", m.Skills.Select(t => t.Name));

这将使用 ", " 分隔符连接所有技能名称。

关于c# - 如何将 value os list 对象组合成一个用逗号分隔的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28673458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com