具有以下僞代碼:使用LINQ獲得與屬性的對象=另一個對象屬性
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列表中。我怎麼弄到的?
謝謝大家 - 所有好的答案!我選擇了最快的答案作爲正確答案,但都很好! – olf