Linq返回WhereOnumerableIterator从Dictionary值匹配?

Put*_*aKg 2 c# linq dictionary

我有以下代码来返回 Dictionary<int, string> buttonGroups 值匹配某些字符串的项目.

    public static void RemoveColorRange(List<Button> buttons, int[] matches)
    {
        Dictionary<int, string> buttonGroups = new Dictionary<int, string>();

        foreach (Button btn in buttons)
        {                               
            if ((int)btn.Tag == matches[0] || (int)btn.Tag == matches[1])
                continue;

            SolidColorBrush brush = (SolidColorBrush)btn.Background;
            Color color = new Color();
            color = brush.Color;
            buttonGroups.Add((int)btn.Tag, closestColor(color));               
        }

        var buttonMatches = buttonGroups.Where(x => x.Value == 'somestring');
    }
Run Code Online (Sandbox Code Playgroud)

但是它返回以下类型而不是字典对象.我似乎无法从buttonMatches中检索任何值.我错过了什么?

{System.Linq.Enumerable.WhereEnumerableIterator<System.Collections.Generic.KeyValuePair<int,string>>}
Run Code Online (Sandbox Code Playgroud)

And*_*rei 5

那是因为Where没有返回字典.要拥有字典,您需要明确地将过滤结果转换为一个:

var buttonMatches = buttonGroups.Where(x => x.Value == 'somestring')
                                .ToDictionary(x => x.Key, x => x.Value);
Run Code Online (Sandbox Code Playgroud)