skip to Main Content

I have dictionary Dictionary<int, List<string>> taskList = new Dictionary<int, List<string>>();

which gives me the out like:

Task ID: 1664003         Values:
                        "2"
                        "5"
                        "1"
                        "4"
                        "3"

Task ID: 1664004         Values:
                        "1"
                        "2"
                        "3"
                        "5"
                        "4"

Task ID: 1664005         Values:
                        "1"
                        "2"
                        "5"
                        "4"
                        "3"

Now I want to search for keys of zero index of pair value like below:

Values: "2"     Task Id: 1664003
Value: "1"      Task Id: 1664004, 1664005

I want to achieve it using lambda expression

3

Answers


  1. You’ll need to iterate over every entry of the dictionary, get the first item of the list and compare it to what you’re looking for, like so:

    var search = 1;
    var dictionary = // your dictionary
    var output = new List<int>();
    
    foreach (var kvp in dictionary)
    {
        if (kvp.Value.First() == search)
            output.Add(kvp.Key);
    }
    
    Console.WriteLine($"The following tasks have the value {search} in the dictionary:");
    foreach (var result in output)
        Console.WriteLine(result);
    

    P.S You might want to rethink your data structure, as this seems like a misuse of a dictionary

    Login or Signup to reply.
  2.             Dictionary<int, List<string>> taskList = new Dictionary<int, List<string>>();
                List<string> list1 = new List<string>() { "2", "5", "1", "4", "3" };
                List<string> list2 = new List<string>() { "1", "2", "3", "5", "4" };
                List<string> list3 = new List<string>() { "1", "2", "5", "4", "3" };
                taskList.Add(1664003, list1);
                taskList.Add(1664004, list2);
                taskList.Add(1664005, list3);
    
                string search = "1";
    
                var result = new List<int>();
    
                foreach (var kvp in taskList)
                {
                    if (kvp.Value[0] == search)
                        result.Add(kvp.Key);
                }
    
                Console.WriteLine($"Output: {string.Join(", ", result)}");
    

    Output:

    Output: 1664004, 1664005
    
    Login or Signup to reply.
  3. Your dictionary is the wrong way round for this kind of search. Consider creating a dictionary that is the right way round, then search it many times

    var d = new Dictionary<int, List<string>>()
    {
       { 1664003, new List<string>() {"2","5","1","4","3"}},
       { 1664004, new List<string>() {"1","2","3","5","4"}},
       { 1664005, new List<string>() {"1","2","5","4","3"}}
    };
    
    //invert the dictionary (only take first element of each list)
    var dRev = d.GroupBy(kvp => kvp.Value.First(), kvp => kvp.Key)
      .ToDictionary(g => g.Key, g => g.ToList());
    
    
    //now you can search it many times
    var x = dRev["1"];     //x is a list containing {1664004,1664005}
    var y = dRev["2"];     //y is a list containing {1664003}
    

    Ideally you can keep your dRev for as long as is reasonably practical; if you have a lot of searches to do in a batch, then rebuilding it makes sense. There’s no point rebuilding it every time you search for a single item, but perhaps consider taking the thing that builds the dictionary you have right now and adding to it so it builds a reverse dictionary too, if you’re going to be searching in this direction often, but for a single item at a time.

    If your searches are only ever in this direction, your dictionary may be the wrong way round entirely and the thing that maintains it should be reworked to reverse it permanently

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search