使用 LINQ 省略字典值部分中的一些条目,并将其投影到维护原始键的新字典中

发布于 2024-08-11 05:01:46 字数 483 浏览 3 评论 0原文

我有一个通用字典,其模板化方式如下:

Dictionary<object, IList<ISomeInterface>> dictionary1 = new Dictionary<object,    IList<ISomeInterface>>();  

如果我想针对任意键省略某些列表项(即组成字典的每个键值对的值部分中包含的列表中的项目) )给定一些任意条件(可以说省略列表项,其中列表项包含字符串“abc”)

我希望能够创建/项目到一个新字典中,该字典将包含包含列表的条目,而没有名称包含“abc”的项目“

如何使用 Lambda 表达式实现此目的?

我正在尝试这样的事情:

var result1 =dictionary1.Where(x => x.Value == x.Value.Select(y => !y.contains("abc"));

I have a generic dictonary which is templated in the following manner:

Dictionary<object, IList<ISomeInterface>> dictionary1 = new Dictionary<object,    IList<ISomeInterface>>();  

If I wanted to omit certain list items against arbitrary keys (that is the items that are in the list contained within the value part of each of the key value pairs making up the dictionary) given some arbitrary condition (lets say omitting list items where a list item contained the string "abc")

I would expect to able to create/project into a new dictionary which would contain entries which contained lists WITHOUT those items whose name contained "abc"

How do I achieve this using a Lambda expression?

I was trying something like this:

var result1 = dictionary1.Where(x => x.Value == x.Value.Select(y => !y.contains("abc"));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一萌ing 2024-08-18 05:01:46

你想要它作为另一本词典吗?试试这个:

var result = original.ToDictionary(
    pair => pair.Key, // Key in result is key in original
    value => pair.Value  // Value in result is query on original value
                 .Where(item => !item.Contains("abc").ToList());

编辑:VB按照评论:

someDictionary.ToDictionary(Function(x) x.key, _ 
            Function(y) y.Value.Where(not y.Value contains("abc").ToList())

To you want it as another dictionary? Try this:

var result = original.ToDictionary(
    pair => pair.Key, // Key in result is key in original
    value => pair.Value  // Value in result is query on original value
                 .Where(item => !item.Contains("abc").ToList());

EDIT: VB as per the comment:

someDictionary.ToDictionary(Function(x) x.key, _ 
            Function(y) y.Value.Where(not y.Value contains("abc").ToList())
雪若未夕 2024-08-18 05:01:46
var result1 = dictionary1.Select(x => 
     {
          return new KeyValuePair<object, IList<ISomeInterface>>(
                     x.Key, 
                     x.Value.Where(y => y.Contains("abc")).ToList());
     }).ToDictionary(item => item.Key, item => item.Value);
var result1 = dictionary1.Select(x => 
     {
          return new KeyValuePair<object, IList<ISomeInterface>>(
                     x.Key, 
                     x.Value.Where(y => y.Contains("abc")).ToList());
     }).ToDictionary(item => item.Key, item => item.Value);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文