0

I have the following datatable: enter image description here

I want to split it into List that will contains 3 tables like this: enter image description here

What is the best and the fastest way? I have only loop in my head, but I think this is not the best idea, because my source table contains more than 3k rows and I want to get about 300 sub tables...

Bryuk
  • 3,295
  • 10
  • 45
  • 74
  • Maybe you can try "LinqToSQL", Please read this article : http://www.codeproject.com/Articles/26657/Simple-LINQ-to-SQL-in-C – Transcendent Dec 18 '13 at 18:59
  • Since these are not indexed database tables, you will have to execute a check on every single row, although it doesn't necessarily have to be a loop. – Scampbell Dec 18 '13 at 19:00
  • I'm not so familiar with Linq, so can you provide a code for me? I will definitely spend some time reading Linq=) – Bryuk Dec 18 '13 at 19:07
  • In your source data table, are Name1, Name2, Name3 just values in a specific column of the DataTable, similar to Text1, Text2, and Text3? – Scampbell Dec 18 '13 at 19:13
  • My DataTable looks exactly like in my example. First Column Contains Names, then starts from next row after each name goes rows with data, then again row with name and... – Bryuk Dec 18 '13 at 19:24
  • 1
    If the name column were filled in for every row, [this answer](http://stackoverflow.com/questions/12740353/split-a-datatable-into-2-or-more-datatables-based-on-column-value?rq=1) could be of help. – Scampbell Dec 18 '13 at 19:28
  • No, it doesn't... I did export of this table from excel and my table looks EXACTLY like on my example – Bryuk Dec 18 '13 at 19:29
  • Upvotes appreciated =) – Bryuk Dec 18 '13 at 20:24

1 Answers1

1

Thanks for everybody. I've end up with my solution that I combined from your comments=)

// Fill Employee names in each row
string fullName = "";
for (int i = 0; i < dt.Rows.Count; i++)
{
    if (dt.Rows[i][0].ToString() != "")
    {
        fullName = dt.Rows[i][0].ToString();
    }
    else
    {
        dt.Rows[i][0] = fullName;
    }
}

// Split into tables by each employee
List<DataTable> employeesTables = dt.AsEnumerable()
                        .GroupBy(row => row.Field<string>("F1"))
                        .Select(g => g.CopyToDataTable())
                        .ToList();
Bryuk
  • 3,295
  • 10
  • 45
  • 74