具有以下伪代码:
class Foo
{
public int id { get; set; }
public string info { get; set; }
}
class Bar
{
public int id { get; set; }
public int FooId { get; set; }
public string moreInfo { get; set; }
}
IList<Foo> foos = new List<Foo>();
foos.Add(new Foo() { id = 1, info = "first!" });
foos.Add(new Foo() { id = 2, info = "second!" });
foos.Add(new Foo() { id = 3, info = "third!" });
IList<Bar> bars = new List<Bar>();
bars.Add(new Bar() { id = 1, FooId = 1, moreInfo = "Something" });
bars.Add(new Bar() { id = 2, FooId = 1, moreInfo = "else" });
bars.Add(new Bar() { id = 3, FooId = 2, moreInfo = "here" });
bars.Add(new Bar() { id = 4, FooId = 6, moreInfo = "and" });
bars.Add(new Bar() { id = 5, FooId = 7, moreInfo = "here as well" });
var returnValue = select bar from bars
where bar.FooId IN foos.Id
我要实现的是一个列表(我的returnValue),其中包含ID为1、2和3的小节,因为它们的FooId在foos列表中。我该怎么办?
最佳答案
var returnValue = bars.Where(b => foos.Select(f => f.Id).Contains(b.FooId);
要么
var returnValue = from b in bars
join f in foos on b.FooId equals f.Id
select b;
关于c# - 使用LINQ获取具有属性=另一个对象属性的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24260236/