-1

How can I split a single column to multiple columns in c#?

For example: I have a table with a single column that looks like this:

Acceleration100
From 2010-01-04 To 2015-06-03
From 2015-06-22 To 2015-06-23
Acceleration20
From 2010-01-14 To 2015-08-03
From 2015-09-22 To 2015-12-23
Acceleration200
From 2010-11-04 To 2015-06-13
From 2015-02-22 To 2015-06-29

I want it to look like this:

Acceleration100                | Acceleration20                | Acceleration200
From 2010-01-04 To 2015-06-03  | From 2010-01-14 To 2015-08-03 | From 2010-11-04 To 2015-06-13
From 2015-06-22 To 2015-06-23  | From 2015-09-22 To 2015-12-23 | From 2015-02-22 To 2015-06-29

What should I do?

s_baldur
  • 29,441
  • 4
  • 36
  • 69
  • 2
    This does not make sense. How your data are represented in C#? Please provide a clearer picture. – panoskarajohn Dec 03 '19 at 14:42
  • 1
    With no specific data structure? From one display to an other without going to real date time type or anything? – xdtTransform Dec 03 '19 at 14:43
  • 1
    `What should I do?` First, you should try to come up with a solution on your own. If you get stuck, come back here, show us the code you have written, and then we can help. – Casey Crookston Dec 03 '19 at 14:44

1 Answers1

0

Create a real data structure like Dictionary<string, List<string>> where the key is the Acceleration XYZ. The parse your file lines by lines. If you encounter a line with Acceleration100 you insert it as a key then all following value will be added to the list.

This way you may have multiple occurrences of Acceleration100 under the same key.

You might want to change List<string> to a more robust structure with DateTime properties: StartDate, EndDate. Perhaps even check if the "From To" line is in the correct format.

Then the projection to column can be done like: C#: Bind Dictionary<string, List<string>> to DataTable

Then you can format the output like :Print Contents Of A DataTable

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
xdtTransform
  • 1,986
  • 14
  • 34