LINQ - IEnumerable.Join on Anonymous Result Set in VB.NET

Posted by user337501 on Stack Overflow See other posts from Stack Overflow or by user337501
Published on 2010-05-28T20:59:55Z Indexed on 2010/05/28 21:02 UTC
Read the original article Hit count: 858

Filed under:
|
|

I've long since built a way around this, but it still keeps bugging me... it doesnt help that my grasp of dynamic LINQ queries is still shakey.

For the example:

Parent has fields (ParentKey, ParentField)
Child has fields (ChildKey, ParentKey, ChildField)
Pet has fields (PetKey, ChildKey, PetField)

Child has a foreign key reference to Parent on Child.ParentKey = Parent.ParentKey
Pet has a foreign key reference to Child on Pet.Childkey = Child.ChildKey

Simple enough eh?

Lets say I have LINQ like this...

Dim Q = FROM p in DataContext.Parent _
Join c In DataContext.Child On c.ParentKey = p.ParentKey

Consider this a "base query" on which I will perform other filtering actions. Now I want to join the Pet table like this:

Q = Q.Join(DataContext.Pet, _
Function(a) a.c.ChildKey, _
Function(p As Pet) p.ChildKey, _
Function(a, p As Pet) p.ChildKey = a.c.ChildKey)

The above Join call doesnt work. I sort of understand why it doesnt work, but hopefully it'll show you how I tried to accomplish this task.

After all this was done I would have appended a Select to finish the job.

Any ideas on a better way to do this? I tried it with the PredicateBuilder with little success. I might not know how to use it right but it felt like it wasnt gonna handle the joining.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about vb.net