c# - 在 FindAll 函数中使用 List -C# LinQ

标签 c# entity-framework linq

我有以下代码行来过滤列表

items = ctx.listName.ToList().FindAll(x=>x.LocationId= locationId); //int locationId passed as parameter

现在,有没有一种方法可以拥有多个位置并在查询中使用它们,而不是只有一个位置 (locationId)?

点赞而不是int locationId我们可以使用 List<int> locations并有类似的东西

  items = ctx.listName.ToList().FindAll(x=>x.LocationId in locations)

最佳答案

您可以像这样使用 LINQ Contains() 扩展方法。

C# 中的 Linq Contains 方法用于检查序列或集合(即数据源)是否包含指定元素。如果数据源包含指定元素,则返回 true,否则返回 false

List<int> locations= // code to get location id's here

items = ctx.listName.Where(x=>locations.Contains(x.locationId)).ToList();

关于c# - 在 FindAll 函数中使用 List -C# LinQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68148756/

相关文章:

c# - c# 中的 Baby-Step Giant Step 算法。哈希表内存不足

c# - 编写 C++ .dll 并访问它们 Unity-3D

c# - 如何使用左连接、聚合和 where 子句创建 Linq to Sql

asp.net - 来自数据库的 Entity Framework 更新模型不为 tt 类中新添加的表生成 cs 类

c# - 使用 LINQ 对包含列表和更新对象的对象进行排序

.net - 在 where 子句中使用 Array 进行 Linq 查询?

c# - ASP.NET 用户/角色编辑器

c# - C#中的命名问题

c# - 使用继承自 IdentityDbContext 的上下文对 Entity Framework 6 进行单元测试

c# - Linq 按对象分组然后选择