2

I have the following code to return items from Dictionary<int, string> buttonGroups where the values match certain string.

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

However it returns the following type instead of a dictionary object. I can't seem to retrieve any value from buttonMatches. What am I missing?

{System.Linq.Enumerable.WhereEnumerableIterator<System.Collections.Generic.KeyValuePair<int,string>>}
4

1 回答 1

5

那是因为Where不返回字典。要拥有一本字典,您需要将过滤结果显式转换为一个:

var buttonMatches = buttonGroups.Where(x => x.Value == 'somestring')
                                .ToDictionary(x => x.Key, x => x.Value);
于 2013-06-24T15:36:43.607 回答