从索引范围内的字符串列表中连接字符串

时间:2018-07-07 08:17:05

标签: c# linq

我有一个很大的字符串列表,并且我需要另一个包含string.string的列表,该列表由前300个索引的逗号组成,之后是另外300个索引,依此类推..直到所有字符串完成。 我在这里展示我的工作。

    List<string> tempSMSNoList = SMSNos.Split(',').ToList(); // 938
       if (tempSMSNoList.Count > 300)
        {
           int Quotient = tempSMSNoList.Count / 300;   // 3
           int Remainder = tempSMSNoList.Count % 300;  // 38

           List<string> myNewString = new List<string>(); // Need to store new value in this list.
           for(int i=0; i < Quotient; i++)
           {
             // Logic here..
           }
        }

2 个答案:

答案 0 :(得分:1)

首先,由于if (!defined('SECURE')) { die(); } Quotient类型,而不是包含int属性的类,因此您的代码不应编译。

第二,有一个Linq CountSkip方法可以帮助收集新的值。

Take

答案 1 :(得分:0)

您可以为此使用LINQ

var result = new List<string>();
int groupsize = 300;
for (int i = 0; i< tempSMSNoList.Count(); i+= groupsize)
    result.Add(String.Join(",", tempSMSNoList.Skip(i).Take(groupsize));