简体   繁体   English

将2 List <string>与LIKE子句进行比较

[英]Compare 2 List<string> with a LIKE clause

I have 2 lists, one a list of file names, the second a list of file name stubs, i would like to select everything from the first list where the file name is like the file name stub. 我有2个列表,一个是文件名列表,第二个是文件名存根列表,我想从第一个列表中选择文件名就像文件名存根一样的所有内容。

List<string> files = new List<string>() {"a.txt", "b.txt", "c.txt"};
List<string> fileStub = new List<string>() {"a", "b", "c"};

The query would return all records from the first list. 该查询将返回第一个列表中的所有记录。

Thanks in advance. 提前致谢。

var results = files.Where(x => fileStub.Any(y => x.Contains(y))).ToList();

if the order is important (of course, with this sample, the size of the two lists is important, if you don't want IndexOutOfRange exceptions) 如果订单很重要(当然,使用此示例,如果您不希望IndexOutOfRange例外,则两个列表的大小很重要)

var res = files.Where((file, index) => file.Contains(fileStub[index]));

if you don't mind order (than list sizes are not important) 如果你不介意订单(比列表大小不重要)

var res = files.Where(file => fileStub.Any(fs => file.Contains(fs)));
var result = files.Where(item => fileStub.Any(stub => item.Contains(stub))).ToList();

Use the Any and Contains methods. 使用AnyContains方法。

List<string> files = new List<string>() {"a.txt", "b.txt", "c.txt"};
List<string> fileStub = new List<string>() {"a", "c"};
var result = files.Where(x => fileStub.Any(y => x.Contains(y))).ToList();

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

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