如何在MCV C#中用逗号分隔字符串 [英] How do I separate strings by a comma in MCV C#
本文介绍了如何在MCV C#中用逗号分隔字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在MVC项目视图中以逗号分隔返回名称的列表.我正在努力实现目标,我感到这很容易.
I need to separate a list of returned names by comma in my MVC project View. I''m struggling to achieve and i have a feeling its an easy job.
var names = dbcontext.EmployeeTable.Select(x => x.Name).ToList();
在我看来,我正在像这样遍历该对象:
in my view i''m looping through this object like this:
@foreach(e in names)
{
//i need to separate the names by a comma i.e
// john, jen, glen
}
可以帮忙吗?
谢谢
我尝试过的事情:
我尝试使用string.join(," name),但是由于某些原因,这不起作用
could some please help?
Thank you
What I have tried:
I have tried to use string.join(","name) but for some reason this is not working
推荐答案
最简单的选项是 ^ ]:
The simplest option would be string.Join[^]:
string nameList = string.Join(", ", names);
作为变体:
As variant:
public string AllNamesSeparatedByComma(List<string> names)
{
StringBuilder allNames = new StringBuilder();
foreach(string e in names)
{
allNames.AppendFormat("{0},", e);
}
return allNames.ToString();
}
使用聚合
Use Aggregate
string nameList = names.Aggregate((a, b) => a + ", " + b);
这篇关于如何在MCV C#中用逗号分隔字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文