Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SelectMany to Dictionary problems

Tags:

c#

linq

I am struggling with a Linq statement to get get a list of items from within a list of items and convert to a dictionary.

I have a list of "Windows" that have a list of "Controls" but one control type also has a list of controls and I cant seem to figure it out (and just when I thought I understood Linq:))

The current working nested foreach loop

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

My rediculous attempt at a Linq query to do the same. (which does not work)

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

If someone could point me in the right direction to get this Linq statement working it would be awesome :)

Here is some mock objects to help you help me, 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; }
}

Thanks

like image 281
sa_ddam213 Avatar asked May 24 '26 08:05

sa_ddam213


1 Answers

The following should work:

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

But Hamlet has a valid point: Each control inside a GUIGroup can be another GUIGroup. You need recursion to solve this:

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);
like image 78
Daniel Hilgarth Avatar answered May 27 '26 03:05

Daniel Hilgarth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!