问题
I have a List< Dictionary < string, object >>
variable as follows.
private static List<Dictionary<string, object>> testData = new List<Dictionary<string, object>>(100);
// Just Sample data for understanding.
for (int i = 0; i < 100; i++)
{
var test = new Dictionary<string, object>
{
{ "aaa", "aaa" + i % 4 },
{ "bbb", "bbb" + i % 4 },
{ "ccc", "ccc" + i % 4 },
{ "ddd", "ddd" + i % 4 },
{ "eee", "eee" + i % 4 },
{ "fff", "fff" + i % 4 },
{ "ggg", "ggg" + i % 4 },
{ "hhh", "hhh" + i % 4 },
{ "iii", "iii" + i % 4 }
};
testData.Add(test);
}
I want to search for a list of key,value in the Dictionary and return the List< Dictionary < string, object >>
contains the searchPattern I passed.
Dictionary<string, object> searchPattern = new Dictionary<string, object>();
searchPattern .Add("aaa", "aaa4");
searchPattern .Add("eee", "eee2");
searchPattern .Add("fff", "fff1");
searchPattern .Add("ddd", "ddd3");
public List<Dictionary<string, object>> SearchList(List<Dictionary<string, object>> testData, Dictionary<string, object> searchPattern)
{
List<Dictionary<string, object>> result;
// Search the list.
return result;
}
Any other suggestions to search also appreciated. Many Thanks!!
回答1:
This will return the first dictionary in the list that contains all the key-value pairs in search pattern, or null
if there isn't one.
public Dictionary<string, object> SearchList
(
List<Dictionary<string, object>> testData,
Dictionary<string, object> searchPattern
)
{
return testData.FirstOrDefault(x => searchPattern.All(x.Contains));
}
If you want all the matches (not just the first one) use Where([...]).ToList()
instead of FirstOrDefault([...])
.
回答2:
Let me try:
public static List<Dictionary<string, object>> SearchList(List<Dictionary<string, object>> testData, Dictionary<string, object> searchPattern)
{
return testData.Where(t =>
{
bool flag = true;
foreach (KeyValuePair<string, object> p in searchPattern)
{
if (!t.ContainsKey(p.Key) || !t[p.Key].Equals(p.Value))
{
flag = false;
break;
}
}
return flag;
}).ToList();
}
OR
public static List<Dictionary<string, object>> SearchList(List<Dictionary<string, object>> testData, Dictionary<string, object> searchPattern)
{
return testData
.Where(t => searchPattern.All(p => t.ContainsKey(p.Key) &&
t[p.Key].Equals(p.Value)))
.ToList();
}
回答3:
If I understand you correctly:
var result = testData.Where(dic => searchPattern.All(dic.Contains))
.ToList();
回答4:
There are couple of ways of doing this. _Individuals1 = file1.fileIndividuals; _Individuals2 = file2.fileIndividuals;
foreach (KeyValuePair<int, Individual> kvpInd in _Individuals1)
{
foreach (KeyValuePair<int, Individual> kvpInd2 in _Individuals2)
{
if (kvpInd.Value.name.name == kvpInd2.Value.name.name)
{
similarInds.Add(kvpInd.Key, kvpInd.Value);
}
}
}
var similarInds = file1.fileIndividuals.
Where(kv1 => file2.fileIndividuals.Any(kv2 => kv1.Value.name.name == kv2.Value.name.name)).
ToDictionary(kv => kv.Key, kv => kv.Value);
this will fail if you have duplicate values using linq you can do like this
回答5:
I agree with armen.shimoon, however something like this:
public static List<Dictionary<string, object>> SearchList(this List<Dictionary<string, object>> list,
Dictionary<string, object> searchPattern)
{
return list.Where(item =>
searchPattern.All(x => item.ContainsKey(x.Key) &&
x.Value.Equals(item[x.Key])
)).ToList();
}
And then using like this:
searchResult = testData.SearchList(searchPattern);
来源:https://stackoverflow.com/questions/12399212/search-for-a-value-in-listdictionarystring-object