具有包含的lambda中的C#linq表达式 [英] C# linq expression in lambda with contains
问题描述
我试图利用包含"来模拟旧的SQL(1、2、3、4)中的where id"过滤查询的方式.
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.
但是,如果我的ID更深,在使用它时会遇到一些困难.
However I have some difficulties in using it where my id's are in a deeper level.
代码:
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();
}
}
现在,我正尝试通过使用contains功能从我的收藏中删除"Person A"和"Person F".但是我看不到我在做什么错.
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.
有人可以告诉我我在做什么错吗? 我尝试了不同版本的lambda,这是我可以获得的关闭次数,但是我从表达式中取出了所有6个项目.
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.
推荐答案
您的方法是正确的,但是您应该使用Where
而不是Select
Your way is correct but you should use Where
instead of Select
var selected = persons.Where(p => p.Appearance
.Where(a => listOfSearchedIds.Contains(a.Id))
.Any()).ToList();
然后您需要使用Any
来检查p.Appearance.Where
的返回序列是否包含任何元素.或者可以直接使用Any
并使其更短:
And you need to use Any
to check whether the returning sequence from p.Appearance.Where
contains any element.Or you can use Any
directly and make it shorter:
var selected = persons.Where(p => p.Appearance
.Any(a => listOfSearchedIds.Contains(a.Id))
.ToList();
这篇关于具有包含的lambda中的C#linq表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!