Home > OS >  Dictionary lookup with LinQ to find a subset where the Value is a List element
Dictionary lookup with LinQ to find a subset where the Value is a List element

Time:05-17

The dictionary is in the form that is similar to this:

 var dictionary = new Dictionary<string, List<DummyObject<int, string, string>>>()
            {
                { "one", new List<DummyObject<int, string, string>>()
                        {
                            new DummyObject<int, string, string>("ONE"),
                            new DummyObject<int, string, string>("TWO"),
                            new DummyObject<int, string, string>("THREE"),
                            new DummyObject<int, string, string>("FOUR")
                        }
                },
                { "two", new List<DummyObject<int, string, string>>()
                        {
                            new DummyObject<int, string, string>("TWO"),
                            new DummyObject<int, string, string>("THREE"),
                            new DummyObject<int, string, string>("FOUR")
                        }
                },
                { "three", new List<DummyObject<int, string, string>>()
                        {
                            new DummyObject<int, string, string>("THREE"),
                            new DummyObject<int, string, string>("FOUR")
                        }
                }
            };

The DummyObject:

 public class DummyObject<TEntity, TActionType, TParams>
    {
        private TActionType _actionType;

        public DummyObject(TActionType actionType)
        {
            _actionType = actionType;
        }

        public TActionType ActionType
        {
            get
            {
                return _actionType;
            }
        }
    }

And I am trying to write something that will find all dictionary entries where their value has a list with DummyObject "TWO" in them, or 'FOUR" or basically any possible value that could exist in that field (this is an example of course but image 30 possibilities).

I just need to get the entire subset of the dictionary (in one quick LinQ statement) so then I can do a further reduction based on the key (which is more than a string in reality). So for the example of "TWO" the result should be a dictionary that looks like this:

var subsetDictionary = new Dictionary<string, List<DummyObject<int, string, string>>>()
            {
                { "one", new List<DummyObject<int, string, string>>()
                        {
                            new DummyObject<int, string, string>("ONE"),
                            new DummyObject<int, string, string>("TWO"),
                            new DummyObject<int, string, string>("THREE"),
                            new DummyObject<int, string, string>("FOUR")
                        }
                },
                { "two", new List<DummyObject<int, string, string>>()
                        {
                            new DummyObject<int, string, string>("TWO"),
                            new DummyObject<int, string, string>("THREE"),
                            new DummyObject<int, string, string>("FOUR")
                        }
                }

Just don't seem to be able to find the right combo to get it.

CodePudding user response:

If you need the actual entries try:

dictionary.Where(x => x.Value.Any(x => x.ActionType == "ONE"))

CodePudding user response:

I think you want something like the following:

dictionary
    .Where(entry => entry.Value
        .Any(item => item.ActionType == "TWO"))
    .ToDictionary(
        entry => entry.Key,
        entry => entry.Value);
  • Related