简体   繁体   English

如何在C#Linq中基于数组过滤或查询对象收集

[英]How to filter or query an object collecting based upon an array in C# Linq

I have a C# collection of custom objects, say BookUsers, which has UserID as one of the property. 我有一个自定义对象的C#集合,例如BookUsers,它具有UserID作为属性之一。 I have a string array of UserID's in a separate variable. 我在一个单独的变量中有一个UserID的字符串数组。 I want to get the subset of the BookUsers collection filtered by the UserID's in the string array. 我想通过字符串数组中的UserID来过滤BookUsers集合的子集。 Please let me know how to do this using Linq. 请让我知道如何使用Linq执行此操作。

Thanks 谢谢

bookusers.Where(b=>ids.Contains(b.UserId))

您可能需要考虑将用户ID数组放入HashSet中,以减少内部查询的复杂性。

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

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