Reputation: 872
Having the following pseudo code:
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
What I want to achieve is a list (my returnValue) to contain the bars with id 1, 2 and 3 as their FooId is in the list of foos. How can I get that?
Upvotes: 1
Views: 98
Reputation: 53958
You could try something like this:
var filteredBars = bars.Join(foos, x=>x.FooId, y=>y.id, (x, y) => x);
Upvotes: 3
Reputation: 13380
you can use join clause like this
from bar in bars
join foo in foos on bar.FoorId equals foo.id
select bar
Upvotes: 3
Reputation: 5121
var returnValue = bars.Where(b => foos.Select(f => f.Id).Contains(b.FooId);
Or
var returnValue = from b in bars
join f in foos on b.FooId equals f.Id
select b;
Upvotes: 4