SelectMany to Dictionary问题

时间:2013-01-11 09:42:05

标签: c# linq

我正在努力使用Linq语句来获取项目列表中的项目列表并转换为字典。

我有一个“Windows”列表,其中包含一个“控件”列表,但是一个控件类型也有一个控件列表,我似乎无法弄明白(当我以为我理解Linq时))< / p>

当前工作嵌套的foreach循环

    public static Dictionary<int, Dictionary<int, bool>> _windowControlVisibilityMap = new Dictionary<int, Dictionary<int, bool>>();

    public static void RegisterControlVisibility(IEnumerable<GUIWindow> windows)
    {
        _windowControlVisibilityMap.Clear();
        foreach (var window in windows)
        {
            var controlMap = new Dictionary<int, bool>();
            foreach (var control in window.Controls)
            {
                if (control is GUIGroup)
                {
                    foreach (var grpControl in (control as GUIGroup).Controls)
                    {
                        controlMap.Add(grpControl.ControlId, grpControl.IsWindowOpenVisible);
                    }
                }
                controlMap.Add(control.ControlId, control.IsWindowOpenVisible);
            }

            _windowControlVisibilityMap.Add(window.WindowId, controlMap);
        }
    }

我在Linq查询上做了同样的尝试。 (这不起作用)

    public static Dictionary<int, Dictionary<int, bool>> _windowControlVisibilityMap = new Dictionary<int, Dictionary<int, bool>>();

    public static void RegisterControlVisibility2(IEnumerable<GUIWindow> windows)
    {
        _windowControlVisibilityMap.Clear();
        foreach (var window in windows)
        {
            var dictionary = window.Controls.Select(k => new KeyValuePair<int, bool>(k.ControlId, k.IsWindowOpenVisible))
                .Concat(window.Controls.OfType<GUIGroup>().SelectMany(grp => grp.Controls)
                .Select(k => new KeyValuePair<int, bool>(k.ControlId, k.IsWindowOpenVisible)));

           // _windowControlVisibilityMap.Add(window.WindowId, dictionary);
        }
    }

如果有人能指出我正确的方向让这个Linq声明有效,那就太棒了:)。

这是一些帮助你帮助我的模拟对象,LOL

public class GUIWindow
{
    public int WindowId { get; set; }
    public List<GUIControl> Controls { get; set; }
}

public class GUIControl
{
    public int ControlId { get; set; }
    public bool IsWindowOpenVisible { get; set; }
}

public class GUIGroup : GUIControl
{
    public List<GUIControl> Controls { get; set; }
}

由于

1 个答案:

答案 0 :(得分:3)

以下内容应该有效:

window.Controls.Select(x => new { x.ControlId, x.IsWindowOpenVisible })
      .Concat(window.Controls
                    .OfType<GUIGroup>()
                    .SelectMany(x => x.Controls)
                    .Select(x => new { x.ControlId, x.IsWindowOpenVisible }))
      .ToDictionary(x => x.ControlId, x => x.IsWindowOpenVisible);

但是哈姆雷特有一个有效的观点:GUIGroup内的每个控件都可以是另一个GUIGroup。你需要递归才能解决这个问题:

Func<IEnumerable<GUIControl>, IEnumerable<Tuple<int, bool>> getValues = null;
getValues = x => x.Select(x => Tuple.Create(x.ControlId, x.IsWindowOpenVisible))
                  .Concat(getValues(window.Controls
                                          .OfType<GUIGroup>()
                                          .SelectMany(x => x.Controls)));

getValues(window.Controls).ToDictionary(x => x.Item1, x => x.Item2);