Chris
Chris

Reputation: 2168

Filter Dictionary When Items Don't Exist In Another Dictionary

I have the following scenario - a list of parent items that are the key in a dictionary, and their children, which are the values.

So my dictionary looks like this - Dictionary<ParentModel,List<ChildModel>>

I have a second dictionary that contains looks like this - Dictionary - where the key is an integer value that actually maps to a property in my ChildModel class, and the value is set to true or false based on whether or not I want to use the key in my filter.

So what I'm trying to do is get a list of ParentModels in my dictionary that don't have any children whose ids match the keys in the second dictionary when they key's value is set to true.

Pseudo code -

Dictionary<ParentModel, List<ChildModel>> parentDictionary;
Dictionary<int, bool> selectedChildIds;

parentDictionary.Where(x => !x.Value.Exists(y => selectedChildIds.Where(z => z.Value).Any(d => d.Key == y.SomeProperty)))

Obviously the code is wrong, so I'm looking for assistance with how to achieve my objective using Linq.

Thanks in advance for the help!

Upvotes: 1

Views: 1285

Answers (3)

Ewan
Ewan

Reputation: 1285

using System;
using System.Collections.Generic;
using System.Linq;

namespace UnitTestProject2
{
    public class Class8
    {
        public class ParentModel
        {
        }

        public class ChildModel
        {
            public int Id { get; set; }
        }
        public void GetParentsWithNoChildern()
        {
            Dictionary<ParentModel, List<ChildModel>> parentDictionary = new Dictionary<ParentModel,List<ChildModel>>();
            Dictionary<int, bool> selectedChildIds = new Dictionary<int, bool>();

            List<ParentModel> parentsWithNoChildern = 
                parentDictionary
                .Where(
                    p => p.Value
                            .Where(c => selectedChildIds.Any(sc => sc.Key == c.Id && sc.Value == true))
                            .Count() == 0
                    )
                .Select(k => k.Key)
                .ToList();
        }
    }
}

Upvotes: 0

Josh L.
Josh L.

Reputation: 454

Requirements:

  • List of ParentModel with empty values
  • selectedChilds.ContainsKey(ParentModel.Id)
  • selectedChilds.Value = true

     Dictionary<ParentModel, List<ChildModel>> parentDictionary;
     Dictionary<int, bool> selectedChildIds;
    
     var result = parentDictionary.Where(x => !x.Value.Any() && 
                    selectedChildIds.ContainsKey(x.Key.SomeProperty) && //x.Key.Id?
                    selectedChildIds.Value).Select(x => x.Key).ToList();
    

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460058

So you want all key-value pairs where all children's int-property either don't exist as key in the other dictionary or that corresponding value is false?

var result = parentDictionary
    .Where(kv => kv.Value
        .All(c => !selectedChildIds.ContainsKey(c.SomeProperty) 
               || !selectedChildIds[c.SomeProperty]));

Upvotes: 4

Related Questions