Click here to Skip to main content
15,311,385 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all, I have a nested list, 2D-List, containing "Datagridview" header names (actually attribute names) This is how I fill the lists:

List<List<string>> DscAtt = new List<List<string>>();
for (int i = 1; i < RowCount; i++)
                for (int j = 0; j < i; j++)
                    List<string> Sublist = new List<string>();
                    for (int k = 1; k < ColCount - 1; k++)
                        if (ResultDGV.Rows[i].Cells[k].Value.ToString() != ResultDGV.Rows[j].Cells[k].Value.ToString())

Now, what I want to do is display (console or a rich text box doesn't really matter) first, second, thirth and nth element of each sublist line by line.
For example:
1st items: (a,a,c,d,a,...)
2nd items: (b,b,a,b,...)
3rd items: (c,c,...)
Updated 13-Jan-14 2:32am
OriginalGriff 13-Jan-14 3:52am
What have you tried?
Where are you stuck?
How are you storing them?
What help do you need?
OriginalGriff 13-Jan-14 4:47am
Can you edit your question and put this in there, using a code block round it? I'm pretty sure some stuff is missing from that - it's been swallowed by the HTML.
Use the "Improve question" widget to edit your question.
Karthik_Mahalingam 13-Jan-14 4:09am
post your question completely.

This code: List<list> DscAtt = new List<list>(); will never compile. I assume "((a,b,c),(d,e),(f),(g,h,i,j),(k,l)...)" is an example of your raw data. And, I am going to assume you know how to use either String.Split or RegEx, or whatever, to parse that into a collection of strongly typed lists. If you need help with parsing the string representation of a nested list to a strongly typed nested list, or group of nested lists, ask.

Using this as an example of a generic List of Lists of strings:
List<List<string>> testNestedList = new List<List<string>>
    new List<string>{"a", "b", "c", "d"},
    new List<string>{"e", "f", "g", "h"},
    new List<string>{"i", "j", "k"},
    new List<string>{"l", "m"},
    new List<string>{"1", "2", "3", "4", "5"}
Writing a method/function to return any particular indexed value in every sub-list is easy:
private List<string> SelectSubItems(int ndx, List<List<string>> theList)
    List<string> matches = new List<string>();

    foreach(var subList in theList)
        if (ndx < subList.Count) matches.Add(subList[ndx]);

    return matches;
Note this method will return a List<string>; you can test it like this:
var test = SelectSubItems(4, testNestedList);
Member 12 14-Jan-14 7:05am
I think I'm not able to explain what I mean sorry for that.
The code below displays all lists
foreach (var SubList in DscAtt)
{ foreach (var Item in SubList)
{ ... }
Please check the example:
But I don't want to display lists one by one. I want to display item by item. 1st items of all lists then second one then third ones...
Anyway thx for the help.
bool found = true;
for (int i = 0; found; ++i)
  found = false;
  foreach ( List <string> l in DscAtt)
    if (l.Count > i)
      Console.Write("{0} ", l[i]);
      found = true;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900