开发者

string containing comma delimited values to an ARRAY

开发者 https://www.devze.com 2023-02-11 00:45 出处:网络
Looking at the following code snippet : public JsonResult GetMapList(string siteDescription,string siteStatus)

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

精彩评论

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