Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i got a list
C#
List<List<string>> myList

myList contains 3 lists
list1 {a,b,c}
list2 {d,e,f}
list3 {g,h,k}

how to find index of list that contains string "d"
Posted
Comments
Sergey Alexandrovich Kryukov 29-Jan-16 14:53pm    
What's wrong with just reading original MSDN documentation on this and other classes?
What have you tried so far?
—SA

You could implement finding a match using a for-loop:
C#
private List<string> LoopFindExactMatch(List<List<string>> listolists, string matchtofind)
{
    foreach(var listostring in listolists)
    {
        if(listostring.Contains(matchtofind))
        {
             return listostring;     
        }
    }

    return null;
Or, you could use Linq to simplify the code:
C#
using System.Linq

private List<string> LinqFindExactMatch(List<List<string>> listolists, string matchtofind)
{
    return listolists.FirstOrDefault(ll => ll.Contains(matchtofind));
}
Test:
C#
private void testFindMatch()
{
    List<string> list1 = new List<string> { "a", "b", "c" };
    List<string> list2 = new List<string> { "d", "e", "f" };
    List<string> list3 = new List<string> { "g", "h", "k" };

    List<List<string>> ListOLists = new List<List<string>> {list1, list2, list3};

    List<string> result1 = LoopFindExactMatch(ListOLists, "d");

    List<string> result2 = LinqFindExactMatch(ListOLists, "d");

    // test with string not in list

    List<string> result3 = LoopFindExactMatch(ListOLists, "x");

    List<string> result4 = LinqFindExactMatch(ListOLists, "y");
}
Note that in these examples we search for only the first match. If your list of lists contains 'inner' lists with duplicate values, another strategy is indicated.
 
Share this answer
 
v3
Comments
Nguyen Khoa Beo 3-Feb-16 13:38pm    
Linq is a magical solution. thanks a lot
Please see:
List<T>.Find Method (Predicate<T>) (System.Collections.Generic)[^],
List<T>.FindAll Method (Predicate<T>) (System.Collections.Generic)[^];
see also other System.Collections.Generic.List<>.Find* methods:
List<T> Class (System.Collections.Generic)[^].

Of course, your can also explicitly traverse all the elements in a foreach or for loop until you find what you need to.

Alternatively, you can use LINQ:
LINQ (Language-Integrated Query)[^],
LINQ to Objects[^].

Note that it gives you slow result, with the time complexity of O(N). For big sets, it can be prohibitively slow. Then you may need to use the collection classes which give you O(1), such as Dictionary, HashSet and so on:
Dictionary(TKey, TValue) Class (System.Collections.Generic)[^],
HashSet(T) Class (System.Collections.Generic)[^],
SortedDictionary(TKey, TValue) Class (System.Collections.Generic)[^],
SortedList(TKey, TValue) Class (System.Collections.Generic)[^].

See also:
Big O notation — Wikipedia, the free encyclopedia[^],
Time complexity — Wikipedia, the free encyclopedia[^].

—SA
 
Share this answer
 
v7
First of all, there is no solution to your problem because your 3 lists are wrong and must be replaced by:
C#
list1 {"a","b","c"}
list2 {"d","e","f"}
list3 {"g","h","k"}


The job of programmer means that sometimes, you have to get your hands dirty and create yourself what you need. This is such a case.

This case is very simple because using brute force is also the best way to go.
You have to scan your list in order to scan each element of current sub-list, when you get a match, simply return your index and you are done.

I let you write the code as it is probably your assignment.
 
Share this answer
 
Comments
BillWoodruff 29-Jan-16 22:40pm    
Since this person is a newcomer here, why not write a comment asking them to correct the format of the lists, rather than a solution which unnecessarily elaborates your view they don't know how to as a question ? I question your statement: "brute force is the best way to go."
Patrice T 1-Feb-16 13:40pm    
Thank for your comment.
In this case, "brut force" is simply testing all possibilities.
I see that the OP have made no comment, either everything is obvious for hil or he just wanted the source code of solution.

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