Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有这个用于创建对象列表的字符串列表。
我的字符串列表中有 171 个项目,而我的对象列表有 170 个。所以一个没有通过,但我需要弄清楚是哪一个。
幸运的是,可以在每个对象的每个名称中找到所有字符串。这意味着,例如:
string nameObjOne
将等于:
public class myObj { public string myName {get;set;} }
那么如何检查我的第一个列表中的所有字符串是否都位于对象列表中,以便我可以找出哪个不存在?
您可以使用 LINQ选择存在List<string>但没有相应项目的项目:List<myObj>
List<string>
List<myObj>
var results = strings.Except(myObjects.Select(o => o.myName)).ToArray();
之后,您只需检查results数组的长度即可确定是否有这样的项目。
results
由于实现,它是O(n+m)Except解决方案,它使用HastSet.
Except
HastSet