简体   繁体   English

如何使用linq从另一个列表中过滤一个列表

[英]How do i filter one list from another list using linq

I wrote the following query to filter query. 我写了以下查询来过滤查询。

I used First() which is causing the issue I didn't notice earlier because Entity2 is also a collection. 我使用First()导致了我之前没有注意到的问题,因为Entity2也是一个集合。 It only filters the first Id. 它仅过滤第一个ID。

query = query.Where(x => filter.Ids.Contains(x.Entity2.Select(y => y.testId).First()));

Please suggest how I can use contains to check all testId property of Entity2 ? 请建议我如何使用包含检查Entity2的所有testId属性?

Basically x.Entity2.Select(y => y.testId) is list of Ids which i want to check whether they contains in filter.Ids or not. 基本上x.Entity2.Select(y => y.testId)是ID的列表,我要检查它们是否包含在filter.Ids中。

If I understand correctly, filter.Ids and x.Entity2.Select(y => y.testId) are both lists if Ids, and you want to make sure that all Ids from x.Entity2 are also in filter.Ids . 如果我正确理解,则filter.Idsx.Entity2.Select(y => y.testId)都列出了Ids,并且您要确保x.Entity2中的所有ID也都在filter.Ids In that case, you want the following: 在这种情况下,您需要以下内容:

var result = query.Where(x => x.Entity2.Count(y => filter.Ids.Contains(y.testId)) == x.Entity2.Count);

What we are doing here is we are counting for each element of query , the number of Ids that are both in it's Entity2 and in filter.Ids . 我们在这里所做的是为每个query元素计数,即它的Entity2filter.Ids的ID数量。 If that number is equal to the total number of Ids in Entity2 , then we include that in the result. 如果该数目等于Entity2中Ids的Entity2 ,则我们将其包括在结果中。

For your above query you can also use Any() and Contains() both , it will work as According to you filter is collection which has Ids and Entity2 both are also collection , so assuming that i wrote this, 对于您上面的查询,您还可以同时使用Any()Contains() ,它的工作方式如下:根据您的筛选器是具有IDs和Entity2的集合也是collection,因此假设我编写了此代码,

query = query.Where(x => filter.Where(a=> a.Entity2.Any(y=> a.Ids.Contains(y.testId));

but in your query also you can remove First() and can use ToList() but it will be throw OutofMemoryException , if data is large. 但是在查询中,您也可以删除First()并可以使用ToList()但是如果数据很大,它将被抛出OutofMemoryException

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

相关问题 如何使用 LINQ 语句过滤掉与另一个列表中的一个单词匹配的项目? - How can I use a LINQ statement to filter out items that matches one of the words from another List? 使用linq从另一个列表获取的索引值过滤一个列表 - Filter one list using index value obtained from another list using linq 如何有条件地使用Linq将属性从一个列表复制到另一个列表 - How to copy properties from one list to another using Linq conditionally 如何使用 Linq 根据另一个列表过滤列表? - How to filter a list based on another list using Linq? 如何在 LINQ C# 中使用另一个列表过滤列表 - How to filter a list using another list in LINQ C# 使用LINQ将另一个列表过滤掉列表 - Filter Out List with another list using LINQ 如何通过使用 C# 中的 Linq 比较另一个列表中的值来过滤列表? - How to filter a list by comparing a value from another list using Linq in C#? 使用LINQ从另一个列表获取一个列表 - Getting one list from another list using LINQ 如何在使用linq和C#的查询中创建一个包含另一个对象类型列表的列表对象 - How do I create a list object that contains a list of another object type using from within a query using linq and c# 使用LINQ如何从集合实体创建实体的一个特定字段的列表 - Using LINQ how do I create a List of one particular field of an entity from a collection entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM