开发者

string containing comma delimited values to an ARRAY

Looking at the following code snippet :

    public JsonResult GetMapList(string siteDescription,string siteStatus)
    {
        var IDs = new[] { 3,4,5 };
开发者_开发技巧

Can anyone please advise me on the neatest way to replace the hardcoded 3,4,5 part with the variable siteStatus that will contain a dynamic comma separated string like 3,4,5

Thanks in Advance


using System.Web.WebPages;

var IDs = siteStatus.Split(',').Select(n => n.AsInt());

or (probably you'll need also a validation, because this will break on wrong input)

var IDs = siteStatus.Split(',').Select(n => int.Parse(n));


You might need to do a null check around this

string[] values = siteStatus.Split(',');

int[] ids =  Array.ConvertAll<string, int>(values, delegate(string s) { return int.Parse(s); });

or with LINQ

var ints = from m in siteStatus.Split(',')
           select Convert.ToInt32(m);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜