Skip to main content
 首页 » 编程设计

c#之SelectMany 到 Dictionary 问题

2024年02月20日15sky-heaven

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

我有一个“Windows”列表,其中有一个“控件”列表,但一种控件类型也有一个控件列表,我似乎无法弄清楚(就在我认为我理解 Linq 的时候:))

当前工作的嵌套 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 语句正常工作,那就太棒了:)

这里有一些模拟对象可以帮助你帮助我,大声笑

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

谢谢

请您参考如下方法:

以下应该有效:

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

但 Hamlet 有一个有效的观点: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);