LINQ to Entity Framework, Multiple Joins…

Here is a little example to doing multple joins using LINQ with EF:

var query = context.EquipmentBases

  .Where(e => e.Model == model && e.Serial == serial && e.Suffix == suffix)

  .Join(context.TruckBases, e=>e.EquipmentBaseID, t =>t.EquipmentBaseFK, (e,t)=>new{e,t})

  .Join(context.TruckSupplemental_Hitch, et=>et.t.TruckID, h=>h.TruckFK, (et,h)=>new{et,h})

  .Join(context.TrailerPlugTypeLTs, eth=>eth.et.t.TrailerPlugTypeFK, p=>p.ID, (eth, p)=>new{eth,p})

  .Select(r => new { r.eth.et.e.CurbWeight, r.eth.et.e.MaxLoad, r.eth.h.BallRating, r.eth.h.ReceiverRating, r.eth.h.BallSize, r.eth.et.e.GVWR, r.eth.h.BallHeight, r.eth.et.t.IntegratedHitchSystem, r.p.TrailerPlugType });

Note the pattern in the joins. This is what makes it all work.

5 thoughts on “LINQ to Entity Framework, Multiple Joins…

  1. Hi there! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate! He always kept talking about this. I will forward this page to him. Pretty sure he will have a good read. Thank you for sharing!

    Like

Leave a comment