guanyongxin
V2EX  ›  微软

c# 初入门,求问拆分数据的问题,

  •  
  •   guanyongxin · Jun 18, 2015 · 3283 views
    This topic created in 3999 days ago, the information mentioned may be changed or developed.

    比较我现在的string数组里有2W条数据,我想每1000条一组拆出来,处理,我要怎么做呢...脑洞好闭塞啊...

    5 replies    2015-06-18 22:45:42 +08:00
    mwylaoma
        1
    mwylaoma  
       Jun 18, 2015
    split,for循环,if == 1000,处理
    zkd8907
        2
    zkd8907  
       Jun 18, 2015
    同一楼。如果想花式一点的也可以考虑用linq2object

    var all = new string[20000];
    string[] items;

    for (var i = 0; i < 20; ++i)
    {
    items = all.Skip(i).Take(1000).ToArray();
    }
    guanyongxin
        3
    guanyongxin  
    OP
       Jun 18, 2015
    public static void SplitStr(int count,ArrayList list)
    {
    try
    {
    if (count > list.Count) { PutInHtml((string[])list.ToArray(typeof(string))); return; }
    Random r = new Random();
    int z = list.Count / count;
    ArrayList yu = new ArrayList();
    int start = list.Count;
    for (int i = 0; i < start; i++)
    {
    yu.Add(list[i]);
    list.RemoveAt(i);
    if (list.Count % count == 0) { PutInHtml((string[])yu.ToArray(typeof(string))); Thread.Sleep(r.Next(1000, 4500)); break; }
    }
    var all = (string[])list.ToArray(typeof(string));
    string[] items;
    string info = string.Empty;
    for (var i = 0; i < z; ++i)
    {
    items = all.Skip(i).Take(count).ToArray();
    PutInHtml(items);
    Thread.Sleep(r.Next(1000, 4500));
    }
    }
    catch { }
    }

    这样处理了,先把余数解决,之后可以整除的就用楼上的方法
    caoyue
        4
    caoyue  
       Jun 18, 2015
    用 Linq 大概只需要一行
    p = 1000

    Enumerable.Range(0, (list.Count()+p-1)/p).ToList().ForEach(x=>PutInHtml(a.Skip(p*x).Take(p)));
    guanyongxin
        5
    guanyongxin  
    OP
       Jun 18, 2015 via Android
    @caoyue ling不会.....
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2564 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 01:12 · PVG 09:12 · LAX 18:12 · JFK 21:12
    ♥ Do have faith in what you're doing.