How to check string values ​​in a Dictionary <string, List <string [] >> () object?

To keep it simple, I have the following Dictionary, which I fill with an unknown number of rows for each string key. I also have the following list of strings.

var dict = new Dictionary<string, List<string[]>>();
IList<string> headerList = new List<string>();

How to check if a string from a list is a value in a dictionary? My data looks something like this:

 Key           Value
 -----         ------------------
 Car           honda, volks, benz
 Truck         chevy, ford

I need to check, for example, that "honda" contains int dictionary values. I think I need to do something like the following to see if the values ​​contain a list containing a string. Keep in mind that I'm pretty new to C #.

    foreach (string header in headerList)
    {
        // This is wrong, I don't know what to put in the if statement 
        if (dict.ContainsValue(r => r.Contains(header)))
        {
            // do stuff 
        }
    }
+4
source share
2 answers

John Odom is right, you need a List.

, HashSet<string> Diictionary. . Dictionary<string, HashSet<string>>

, , - :

 headerList.Where(x => dict.Values.Any(d => d.Contains(x))).ToList()

.NET Nested Loops vs Hash Lookups

+3

, (, "honda" ), :

bool isInDict = dict.Values
                    .SelectMany(lst => lst)
                    .Any(car => car == "honda");

, , - :

string containingKey = dict.Keys
                           .Where(key => dict[key].Contains("honda"))
                           .FirstOrDefault();

, , :

List<string> containingList = dict.Values
                                  .Where(v => v.Contains("honda"))
                                  .FirstOrDefault();

, , , . true, .

: . , , , . , .

- , . , (.. ) . , .

0

Source: https://habr.com/ru/post/1584570/


All Articles