.net 带有包含的 lambda 中的 C# linq 表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21919695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# linq expression in lambda with contains
提问by codingjoe
I am trying to make use of the 'contains' to simulate the old SQL 'where id in (1,2,3,4)' way of filtering a query.
我正在尝试使用“包含”来模拟旧的 SQL 'where id in (1,2,3,4)' 过滤查询的方式。
However I have some difficulties in using it where my id's are in a deeper level.
但是,在我的 id 处于更深层次的情况下,我在使用它时遇到了一些困难。
Code:
代码:
public class Category
{
public long Id { get; set; }
public string Name { get; set; }
}
public class Characteristica
{
public Category Category { get; set; }
public int Id { get; set; }
public string Value { get; set; }
}
public class Person
{
public string Name { get; set; }
public List<Characteristica> Appearance { get; set; }
}
class Program
{
static void Main(string[] args)
{
var persons = new List<Person>
{
new Person { Name = "Person A", Appearance = new List<Characteristica> { new Characteristica { Id = 22 }, new Characteristica { Id = 5 }, new Characteristica { Id = 12 } }},
new Person { Name = "Person B", Appearance = new List<Characteristica> { new Characteristica { Id = 1 }, new Characteristica { Id = 6 }, new Characteristica { Id = 11 } }},
new Person { Name = "Person C", Appearance = new List<Characteristica> { new Characteristica { Id = 2 }, new Characteristica { Id = 8 }, new Characteristica { Id = 13 } }},
new Person { Name = "Person D", Appearance = new List<Characteristica> { new Characteristica { Id = 2 }, new Characteristica { Id = 5 }, new Characteristica { Id = 10 } }},
new Person { Name = "Person E", Appearance = new List<Characteristica> { new Characteristica { Id = 1 }, new Characteristica { Id = 8 }, new Characteristica { Id = 10 } }},
new Person { Name = "Person F", Appearance = new List<Characteristica> { new Characteristica { Id = 1 }, new Characteristica { Id = 6 }, new Characteristica { Id = 23 } }},
};
var listOfSearchedIds = new List<int> { 22, 23 };
var selected = persons.Select(p => p.Appearance.Where(a => listOfSearchedIds.Contains(a.Id))).ToList();
}
}
Now I am trying to get 'Person A' and 'Person F' out from my collection by using the contains feauture. However I cannot see what I am doing wrong here.
现在我试图通过使用 contains 功能从我的收藏中获取“A 人”和“F 人”。但是我看不到我在这里做错了什么。
Can someone shed some light on what I am doing wrong? I have tried different versions of my lambda and this is the closes I can get, but I am getting all 6 items out from my expression.
有人可以阐明我做错了什么吗?我尝试了不同版本的 lambda,这是我可以得到的关闭,但我从我的表达式中获取了所有 6 个项目。
回答by Selman Gen?
Your way is correct but you should use Whereinstead of Select
你的方法是正确的,但你应该使用Where而不是Select
var selected = persons.Where(p => p.Appearance
.Where(a => listOfSearchedIds.Contains(a.Id))
.Any()).ToList();
And you need to use Anyto check whether the returning sequence from p.Appearance.Wherecontains any element.Or you can use Anydirectly and make it shorter:
并且您需要使用Any来检查从返回的序列是否p.Appearance.Where包含任何元素。或者您可以Any直接使用并使其更短:
var selected = persons.Where(p => p.Appearance
.Any(a => listOfSearchedIds.Contains(a.Id))
.ToList();
回答by Markus
Try the following:
请尝试以下操作:
var listOfSearchedIds = new List<int> { 22, 23 };
var selected = persons
.Where(p => listOfSearchedIds
.Intersect(p.Appearance
.Select(a => a.Id)).Any()).ToList();
By using Intersect, you compare two lists and return the items that are contained in both of them. Intersect uses a HashSet internally and therefore is a very performant way two find the intersection of two sets. Any() returns true if there is at least one item in the resulting list.
通过使用 Intersect,您可以比较两个列表并返回两个列表中包含的项目。Intersect 在内部使用 HashSet,因此是一种非常高效的方法,可以找到两个集合的交集。如果结果列表中至少有一项,则 Any() 返回 true。

