简体   繁体   English

根据行数将数据集拆分为多个表

[英]Splitting of dataset based on number of rows in to multiple tables

I have a situation where I need to Split dataset results in to multiple tables eventually in to an array based on number of rows.我有一种情况,我需要根据行数将数据集结果拆分为多个表,最终拆分为一个数组。 Ex: my dataset has 34 rows with a url column, I need to split 34 rows in to 4 data tables (10,10,10 remaining 4) and eventually add to an array.例如:我的数据集有 34 行和一个 url 列,我需要将 34 行拆分为 4 个数据表(10、10、10 剩余 4 个)并最终添加到一个数组中。 I am using a forms windows application.我正在使用窗体 Windows 应用程序。 Itried something like below however every time i add records to array it adds entire dataset. Itried类似下面但是每次我将记录添加到数组时,它都会添加整个数据集。 Any help would be appreciated.任何帮助,将不胜感激。

 private DataSet Process(DataSet ds)
        {


            string[] Array1 = new string[10];
            string[] Array2 = new string[10];
            string[] Array3 = new string[10];
            string[] Array4 = new string[10];


            int COunt = ds.Tables[0].DefaultView.Count;
            int NoOfArraysToCreate = COunt/10 + 1;

            for (int i = 0; i <= NoOfArraysToCreate; i++ )
            {
                if (i == 0)
                {
                    foreach (DataRow drs in ds.Tables[0].Rows)
                    {
                        List<String> myList = new List<string>();
                        myList.Add(drs["Url"].ToString());
                        Array1 = myList.ToArray();
                    }
                }
                else if (i == 1)
                {
                    foreach (DataRow drs in ds.Tables[0].Rows)
                    {
                        List<String> myList = new List<string>();
                        myList.Add(drs["Url"].ToString());
                        Array2 = myList.ToArray();
                    }
                }
                else if (i == 2)
                {

                    foreach (DataRow drs in ds.Tables[0].Rows)
                    {
                        List<String> myList = new List<string>();
                        myList.Add(drs["Url"].ToString());
                        Array3 = myList.ToArray();
                    }
                }
                else if (i == 3)
                {

                    foreach (DataRow drs in dsURLsList.Tables[0].Rows)
                    {
                        List<String> myList = new List<string>();
                        myList.Add(drs["Url"].ToString());
                        Array4 = myList.ToArray();
                    }
                }

}

It seems you are looping through all DataTable rows for each of your pages.您似乎正在遍历每个页面的所有 DataTable 行。 I would suggest you only loop over your rows once:我建议你只循环一次你的行:

private List<List<string>> Process(DataSet ds, int pageSize)
{
    List<List<string>> result = new List<List<string>>();

    int COunt = ds.Tables[0].DefaultView.Count;
    int NoOfArraysToCreate = COunt / pageSize + 1;

    IEnumerable<DataRow> collection = ds.Tables[0].Rows.Cast<DataRow>(); //I find it easier to work with enumerables as it allows for LINQ expressions as below

    for (int i = 0; i < NoOfArraysToCreate; i++)
    {
        result.Add(collection.Skip(i*pageSize)
                .Take(pageSize)
                .Select(r => r["Url"].ToString())
                .ToList());
    }

    Parallel.ForEach(result, (page) =>
    {
        Parallel.ForEach(page, (url) => {}); // process your strings in parallel
    });

    return result;//I see you convert your string arrays back to DataSet, but since I don't know the table definition, I'm just returning the lists
}
void Main()
{
    // this is just a test code to illustrate my point, yours will be different
    var ds = new DataSet();
    var dt = new DataTable();
    dt.Columns.Add("Url", typeof(string));
    for (int i = 0; i < 34; i++) {
        dt.Rows.Add(Guid.NewGuid().ToString());//generating some random strings, ignore me
    }
    ds.Tables.Add(dt);
    //---------------------------------------
    Process(ds, 10);// calling your method
}

of course there are ways to do it with for loops as well, but I'd leave that for you to explore.当然,也有使用 for 循环的方法,但我将把它留给你去探索。 I would also say hardcoding table numbers into your method usually is considered a code smell, but since I don't know your context I will not make any further changes我还要说将表号硬编码到您的方法中通常被认为是代码异味,但由于我不知道您的上下文,因此我不会做任何进一步的更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM