简体   繁体   English

C# 检查是否列表<t>包含任何其他列表并返回匹配项

[英]C# Check if list<t> contains any of another list and return the matching item

I have a list of strings that I need to find/verify in another list of strings and return those that are found.我有一个字符串列表,我需要在另一个字符串列表中查找/验证并返回找到的字符串。

List<string> strList1 = new List<string>() { "oranges", "apples", "grapes" };
List<string> strList2 = new List<string>() {
   "Joe likes peaches",
   "Mack likes apples",
   "Hank likes raisins",
   "Jodi likes grapes",
   "Susan likes apples"  
};

OK, so this is a bad example but, basically, I want to be able to create a Linq call to find any value in strList1 in one or more elements in strList2 and return which ones were found.好的,所以这是一个不好的例子,但基本上,我希望能够创建一个 Linq 调用,以在strList1的一个或多个元素中查找strList2中的任何值并返回找到的值。

So the results would be a list of strList1 items found in ````strList2```.所以结果将是在````strList2```中找到的strList1项的列表。 Something like:就像是:

List<string> found = { "apples", "grapes" };

My searching hasn't resulted in anything because I'm probably not searching correctly.我的搜索没有任何结果,因为我可能没有正确搜索。 Any help would be great appreciated.任何帮助将不胜感激。 Thanks!谢谢!

I can't guarantee performance, but this can be accomplished using Where and Any .我不能保证性能,但这可以使用WhereAny来完成。

strList1.Where(str => strList2.Any(str2.Contains(str)))

Or for complex objects:或者对于复杂的对象:

objList1.Where(obj => objList2.Any(obj2.property.Contains(obj.property)))

我想这会回答你的问题

strList1.Where(c => strList2.Any(a => a.Contains(c))).ToList();

Many ways to do it.有很多方法可以做到。 Doing linq you could benefit of Intersect , if you first split the "sentences" to "words".如果您首先将“句子”拆分为“单词”,那么执行 linq 您可能会受益于Intersect

List<string> strList1 = new List<string>() { "oranges", "apples", "grapes" };
List<string> strList2 = new List<string>() {
   "Joe likes peaches",
   "Mack likes apples",
   "Hank likes raisins",
   "Jodi likes grapes",
   "Susan likes apples"  
};

List<string> found = strList1.Intersect(strList2.SelectMany(s => s.Split(" "))).ToList(); 

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

相关问题 检查是否列表<t>包含任何其他列表</t> - Check if list<t> contains any of another list 无法检查列表是否包含C#中的选择项 - Can't check whether a list contains a select item in C# C#LINQ如何从另一个列表中的一个列表中找到匹配项,并在另一个列表中检查属性 - C# LINQ How to find matching item from one list in another list and check for the property in the other list 检查文本是否包含字符串c#linq列表中的任何字符串项 - Check if text contains any string item from list of string c# linq 如何检查一个列表是否包含另一个列表C# - how to check that a list contains another list C# 检查列表是否包含其他列表。 C# - Check if list contains another list. C# LINQ 检查列表是否包含另一个列表中的任何项目 mysql 语法错误 - LINQ check if a list contains any item from another list mysql syntax error 检查列表中的任何项是否与另一个列表中的任何项匹配 - Check if any item in a list matches any item in another list 验证列表<t>确保列表中包含带有数据注释 MVC C# 的列表一项</t> - Validating a List<T> To ensure list contains at list one Item with Data Annotations MVC C# 在 C# 中的另一个列表中搜索列表中的项目 - Search item in list in another list in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM