比较两个列表,并从另一个列表中删除包含条目的每个项目 [英] Comparing two lists and removing each item that contains an entry from the other list

查看:131
本文介绍了比较两个列表,并从另一个列表中删除包含条目的每个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上试图将fileManifest中的所有列表项都复制,但是仅将exclusionFilters中的任何项中不包含的列表项复制到新初始化的列表中.除了嵌套的foreach循环之外,该方法也是一种优雅的方法.

I'm trying to essentially copy any list items in fileManifest but only those that don't contain in any of the items from exclusionFilters to a newly initialized list.
I haven't figured out an elegant way to do this other than a nested foreach loop.

一个偶然的人对这个问题有更好的解决方案吗?也许LINQ?

Does someone by chance have a better solution for this problem? Maybe LINQ?

var fileManifest = new List<string>()
{
   @"C:\Test\Directory1\File1.xml",
   @"C:\Test\Directory1\File2.xml",
   @"C:\Test\Directory1\Directory2\File1.xml",
};

var exclusionFilters = new List<string>()
{
  @"Directory2\"
};

var filteredList = new List<string>();

filteredList的预期输出:

  • C:\Test\Directory1\File1.xml
  • C:\Test\Directory1\File2.xml
  • C:\Test\Directory1\File1.xml
  • C:\Test\Directory1\File2.xml

推荐答案

var filteredList = fileManifest.Where(x => exclusionFilters.All(y => !x.Contains(y))).ToList();

说明:

这篇关于比较两个列表,并从另一个列表中删除包含条目的每个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆