LINQ - 的IList和字符串列表之间的搜索 [英] LINQ - search between IList and list of strings
本文介绍了LINQ - 的IList和字符串列表之间的搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不是在LINQ十分精通,我认为这个问题可以用它来解决。
Im not very well versed in LINQ and I think this problem could be solved with it.
我有对象的列表:
IList<Aclass> allADs;
类:
Class:
public static class Aclass
{
private string myName { get; set; }
//And more attributes.
}
和我有一个字符串列表:
And I have a list of strings:
List<string> lstGroups = new List<string>();
此时在我的代码我的两个对象列表和字符串列表都满了。我要的是 ACLASS
,酒店 MYNAME
通过列表的匹配字符串的列表。字符串列表有独特的元素,没有重复。
At this point in my code both my list of objects and the list of strings are full. What I want is a list of Aclass
that matches the property myName
with the strings of the list. the list of strings have unique elements, there is no repetition.
推荐答案
使用包含
:
var res = lstAclass.Where(a => lstGroups.Contains(a.MyName)).ToList();
在 lstAclass
有很多元素,你可以加快由 HashSet的
组名之前运行此查询构建搜索:
When lstAclass
has lots of elements, you can speed up the search by constructing a HashSet
of group names prior to running this query:
var setGroups = hew HashSet<string>(lstGroups);
var res = lstAclass.Where(a => setGroups.Contains(a.MyName)).ToList();
这篇关于LINQ - 的IList和字符串列表之间的搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文