简体   繁体   English

从数字范围创建字符串

[英]Create string from a number range

Is there a way to make this a one liner?有没有办法让它成为单衬里?

List<string> ids = new List<string>(result.Count);
for(int i = 0; i < result.Count; i++)
    ids.Add(i.ToString());
string reportIds = String.Join(",", ids);

I'm pretty sure there's a way with linq, but I can't figure out我很确定 linq 有办法,但我想不通

string.Join() actually takes IEnumerable<object> in one of its overloads so there is no need to call ToString() yourself. string.Join()实际上在其重载之一中采用IEnumerable<object> ,因此无需自己调用ToString() Here is the complete one-liner using Enumerable.Range() :这是使用Enumerable.Range()的完整单行:

var reportIds = string.Join(",", Enumerable.Range(0, result.Count));
string reportIds = String.Join(",",Enumerable.Range(0, result.Count));
string str = String.Join(",", Enumerable.Range(0, count).Select(n => n.ToString()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM