tags:

views:

33

answers:

3

How can I find out if a Linq to SQL entity has grandchildren or not?

Pseudo-code below:

Return From p In dc.Processes Where p.Signers.Count > 0 and p.Signers.Signatures.Count > 0

Obviously I can't run the code above but I need to make sure that all the returning Processes have at least one Signer and that all of those Signers have at least one Signature.

TIA!

+3  A: 

Something like this should work.

... VB.Net ...

Return From p In dc.Processes _
       Where p.Signers.Any(Function(s) s.Signatures.Any()) _
       Select p

... C# ...

return  from p in dc.Processes
        where p.Signers.Any(s=>s.Signatures.Any())
        select p;
Matthew Whited
I haven't done much with LINQ in VB so this might need a few minor tweaks.
Matthew Whited
oh that was just sweet and too simple for me not to realize on my own! :)
EdenMachine
+2  A: 

You may have to do a sub select if your tables are many to many relationship. Something like this should work in this instance

Return (From p In dc.Processes Where p.Signers.Count > 0 And (from t in p.Signers where t.signatures.count) > 0)
Tommy
+2  A: 

The following code is C#, but it will do the trick:

 from p in db.Processes
 where p.Signers.Any(s => s.Signatures.Any())
 select p
Steven