Having trouble with this, I've tried following several examples but I am just not getting it. It makes perfect sense using the non-lambda way, but how do I do a join using lambda expressions?
var myCats = GetAllCats();
var myHouses = GetAllHouses();
// pseudosql: select * from a inner join b on a.id = b.id
I have tried this:
var fullData = myCats.Join(myHouses, a => a.id, b => b.id, (a, b) => a);
which I kind of got through looking at other examples on SO, but fullData is type IEnumerable<Cat>
so I can't pull any properties from Houses off it.
1); var join = query1. Join(query2, x => x. ParentId, y => y. ParentId, (query1, query2) => new { query1 , query2 }).
In a LINQ query expression, join operations are performed on object collections. Object collections cannot be "joined" in exactly the same way as two relational tables. In LINQ, explicit join clauses are only required when two source sequences are not tied by any relationship.
var fullData = myCats.Join(
myHouses,
cat => cat.id,
house => house.id,
(cat, house) =>
new
{
Cat = cat,
House = house
});
Access via fullData.First().Cat...
or fullData.First().House...
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With