5

我有一个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 中搜索键、值的列表并返回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;
}

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

4

5 回答 5

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

    }

或者

    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();
    }
于 2012-09-13T04:14:16.580 回答
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([...]).

于 2012-09-13T04:21:25.937 回答
1

如果我理解正确:

var result = testData.Where(dic => searchPattern.All(dic.Contains))
                     .ToList();
于 2012-09-13T04:30:38.813 回答
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 有重复的值,这将失败,您可以这样做

于 2012-09-13T04:20:07.637 回答
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);
于 2012-09-13T04:34:12.850 回答