V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
guanyongxin
V2EX  ›  微软

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

  •  
  •   guanyongxin · 2015-06-18 11:18:28 +08:00 · 2405 次点击
    这是一个创建于 3241 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    5 条回复    2015-06-18 22:45:42 +08:00
    mwylaoma
        1
    mwylaoma  
       2015-06-18 12:45:25 +08:00
    split,for循环,if == 1000,处理
    zkd8907
        2
    zkd8907  
       2015-06-18 12:51:57 +08:00
    同一楼。如果想花式一点的也可以考虑用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
       2015-06-18 15:11:56 +08:00
    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  
       2015-06-18 20:37:51 +08:00
    用 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
       2015-06-18 22:45:42 +08:00 via Android
    @caoyue ling不会.....
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2352 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:21 · PVG 21:21 · LAX 06:21 · JFK 09:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.