vb.net - Filter RHS items in LINQ Group Join -
in linq, need left join (group join
) 2 lists of objects using some criteria able expressed equijoins (so can use on
clause), , other criteria relative.
in sql, equivalent (using ludicrous example, want list each person of pets own born on or after owner's wedding date; note data in objects, not in sql):
select pers.name, pers.address, pers.gender, pet.name, pet.species person pers left join pets pet on pers.name=pet.ownername , pet.birthdate >= pers.weddingdate
here's i've got far - doesn't include restriction pets in list should have been born on or after owner's wedding date.
dim res = ( p in person group join pt in pets on p.name equals pt.ownername petlist = group select p, petlist)
first thought use where
clause, individual pets in list aren't in scope @ point.
then thought perhaps answer lambda function in pets list, like
group join pt in pets.where(function(pt) pt.birthdate >= pers.weddingdate)
but don't know (i) how person lambda function scope, or (ii) if do, work anyway.
so, question is, how can filter right-hand-side results of linq group join based on non-equijoin criteria?
i know can of above in join
(e.g. not group join
) context, need (a) return persons without pets match criteria, , (b) return single object per person, pets list.
i have feeling answer this, don't know enough linq generalise it.
the approach i'm taking in case filter collection within select
clause, using function i've had add pet model support join filtering. of course undermines that's great linq (i.e. ability perform arbitrary queries on objects):
dim res = ( p in person group join pt in pets on p.name equals pt.ownername petlist = group select p, thispetlist = petlist.where(function(x) x.mycustomfunction(p.weddingdate)) )
the function i've added pet model accepts date, compares pet's birthdate, , returns true if input date earlier pet.birthdate.
urgh.
Comments
Post a Comment