开发者

VB.NET LINQ group join followed by another group

开发者 https://www.devze.com 2023-04-08 04:43 出处:网络
I have two collections of objects in VB.NET that I want to link together using a join, and possibly group together.Basically my objects look like this:

I have two collections of objects in VB.NET that I want to link together using a join, and possibly group together. Basically my objects look like this:

Institution

  • ID
  • Name

Visit

  • ID
  • InstitutionID
  • Date
  • IsFollowUp
  • IsSelfScheduled

A visit has to be to an institution, but an institution can also have no visits. I can link them together using LINQ, but I can't quite get the institutions that do not have any visits to appear in the li开发者_运维问答st also (which is what I am looking for). I know I have to use a group join, but I can't work out how to incorporate the existing join as well.

From p In visitList Where p.IsFollowUp = False AndAlso p.IsSelfScheduled = False
Group p By Key = p.InstitutionID Into grp = Group
Select InstitutionID = Key, Visits = grp, LastVisitDate = grp.FirstOrDefault().ProvisionalDate

If the worst comes to the worst, I can implement a private class to do what I want, but it seems like it should be something simple in LINQ.

Edit:

Okay, using the link below that Tim posted, I managed to come up with something like this:

From i In institutionList
Select InstitutionID = i.ID, Name = i.Name, Inspections = 
(
   From p In visitList Where p.InstitutionID = i.ID Select p
)

It uses a sub-query to pull out the relevant information, but whether it is efficient or not, I have no idea. It seems to pull out the relevant information.

0

精彩评论

暂无评论...
验证码 换一张
取 消