Click here to Skip to main content
16,011,120 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
when we use a list we can call use them like Somelisr[0] Somlist[1] by point the index but how can i use the data in list<list> any one guide me please

or anyone have an easyiest way to use it , Please Shere i really need to know here us my code

C#
List<list><string>> Wayfinder = new List<list><string>>();

        Wayfinder.Add(new List<string> { "a", "b" });
        Wayfinder.Add(new List<string> { "c", "d" });
        Wayfinder.Add(new List<string> { "e", "f" });
        Wayfinder.Add(new List<string> { "g", "h","i","j" });


PS; well, i have to get each of it data and use intersec or union to find the same data in those lise .

code block added
Posted
Updated 5-May-14 5:03am
v3
Comments
Sergey Alexandrovich Kryukov 5-May-14 10:51am    
Please format the code properly; it does not show the list type. Is it System.Collections.Generic.List<>? where is the generic parameter?
The problem is not clear. What do you want to achieve?
—SA

1 solution

If what you mean is:
C#
List<list><string>> Wayfinder = new List<list><string>>();

Wayfinder.Add(new List<string> { "a", "b" });
Wayfinder.Add(new List<string> { "c", "d" });
Wayfinder.Add(new List<string> { "e", "f" });
Wayfinder.Add(new List<string> { "g", "h","i","j" });</string></string></string></string></string></list></string></list>

then you can find the union easily by using Linq:
C#
List<string> union = new List<string>();
foreach (List<string> list in Wayfinder)
    {
    union.AddRange(list);
    }
union = union.Distinct().OrderBy(u => u).ToList();</string></string></string>

Intersection is much the same:
C#
List<string> intersection = Wayfinder[0];
for (int i = 1; i < Wayfinder.Count; i++)
    {
    intersection = intersection.Intersect(Wayfinder[i]).ToList();
    }</string>
But in your example, it will be an empty list.
 
Share this answer
 
Comments
Real_Criffer 5-May-14 12:46pm    
i want to know what OrderBy(u => u) mean

thank you for you help Sir.
OriginalGriff 5-May-14 14:02pm    
It's a Linq method which sorts the IEnumerable input using a (very) basic lambda expression: it basically saying "sort by: each element itself" rather than by any processing of the element.
I assume you are still learning? If so, you'll get to Linq and Lambda expressions later! Just "bleep" over it for the moment and accept it works... :laugh:
Real_Criffer 6-May-14 12:35pm    
็haha Thx OrifinalGriff btw i try useing ur solution but it the result was system.generic something any clue ><'
PS: I am learning in the last year in the university .. i really need to know to work on my Project thank you for u advise sir
OriginalGriff 6-May-14 13:56pm    
Check your "using" statements: make sure you have

using System.Linq;

in the list.
Real_Criffer 7-May-14 1:23am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
List<List<string>> myList = new List<List<string>>();
myList.Add(new List<string> { "a", "b" });
myList.Add(new List<string> { "c", "d", "e" });
myList.Add(new List<string> { "qwerty", "asdf", "zxcv" });
myList.Add(new List<string> { "a", "b" });

// To iterate over it.
foreach (List<string> subList in myList)
{
foreach (string item in subList)
{
Console.WriteLine(item);
}
}
List<string> union = new List<string>();
foreach (List<string> list in myList)
{
union.AddRange(list);
}
union = union.Distinct().OrderBy(u => u).ToList();
Console.WriteLine(union);
//Console.WriteLine(myList[1]);
Console.ReadLine();
}
}
}
this is all what i have did i do something wrong ?

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