使用 LinQ 查找字典以查找值是列表元素的子集

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

字典的格式类似于:

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

虚拟对象:

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

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

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

我正在尝试编写一些东西来查找所有字典条目,其中它们的值有一个列表,其中包含 DummyObject“TWO”,或“FOUR”或基本上可能存在于该字段中的任何可能值(这是当然是一个例子,但想象 30 种可能性)。

我只需要获取字典的整个子集(在一个快速的 LinQ 语句中),然后我就可以根据键(实际上不仅仅是一个字符串)进行进一步的缩减。所以对于“TWO”的例子,结果应该是一个看起来像这样的字典:

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

只是似乎找不到合适的组合来获得它。

如果您需要实际条目,请尝试:

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

我想你想要像下面这样的东西:

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