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
2 answers
, (, "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