简体   繁体   English

使用 LinQ 进行字典查找以查找 Value 是 List 元素的子集

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

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

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).我只需要获取字典的整个子集(在一个快速的 LinQ 语句中),然后我就可以根据键进行进一步的缩减(实际上这不仅仅是一个字符串)。 So for the example of "TWO" the result should be a dictionary that looks like this:因此,对于“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")
                        }
                }

Just don't seem to be able to find the right combo to get it.只是似乎无法找到合适的组合来获得它。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM