在List <dictionary <string,object =“”>&gt; </dictionary <string,>中搜索值

时间:2012-09-13 03:59:31

标签: c# list search .net-4.0 dictionary

我有一个List< Dictionary < string, object >>变量,如下所示。

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);
}

我想在Dictionary中搜索key,value的列表并返回List< Dictionary < string, object >>包含我传递的searchPattern。

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;
}

任何其他搜索建议也表示赞赏。 非常感谢!!

5 个答案:

答案 0 :(得分:1)

让我试试:

    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();
    }

答案 1 :(得分:1)

这将返回列表中包含搜索模式中所有键值对的第一个字典,如果没有,则返回null

public Dictionary<string, object> SearchList
(
    List<Dictionary<string, object>> testData,
    Dictionary<string, object> searchPattern
)
{
    return testData.FirstOrDefault(x => searchPattern.All(x.Contains));
}

如果您想要所有匹配项(不仅仅是第一项),请使用Where([...]).ToList()代替FirstOrDefault([...])

答案 2 :(得分:1)

如果我理解正确的话:

var result = testData.Where(dic => searchPattern.All(dic.Contains))
                     .ToList();

答案 3 :(得分:0)

有几种方法可以做到这一点。    _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); 

如果您使用linq重复值,则会失败,您可以这样做

答案 4 :(得分:0)

我同意armen.shimoon,不管这样:

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();
    }

然后像这样使用:

    searchResult = testData.SearchList(searchPattern);