I'm trying to figure out how to perform a LEFT JOIN in Linq but struggling...
I have an existing query
Dim people As List(Of SimplePerson)
Using gpd As New peopleDatabase
people = (From person In gpd.People Join office In gpd.Offices On person.office_ref Equals office.officeID
Join personphone In gpd.x_PersonPhone On person.personId Equals personphone.person_ref
Join telephoneNumber In gpd.TelephoneNumbers On personphone.telephoneNumber_ref Equals telephoneNumber.telephoneNumberId
Where (person.preferredGivenName & " " & person.familyName).StartsWith(searchTerm) And telephoneNumber.x_TelephoneNumberTelephoneNumberType.FirstOrDefault.b_telephoneNumberType.telephoneNumberTypeId = 1 And person.active = True
Select New SimplePerson() With {
.Name = person.preferredGivenName & " " & person.familyName,
.EmailAddress = person.emailAddress,
.Title = person.jobTitle_l,
.ResId = person.sourceKey,
.OrganisationId = person.organisation_ref.Value,
.OfficeName = office.name_l,
.TelephoneNumber = telephoneNumber.formattedNumberInternational
}
).OrderBy(Function(n) n.Name).Take(20).ToList
End Using
Return people
For context, this is a query I'm using for a jQueryUI Autocomplete widget. SimplePerson
is a POCO I'm using which just contains the set of properties listed in the code above.
What I'm trying to achieve is to change the query so that it lists people regardless of whether they have any phone number i.e. they have no entries in the table x_PersonPhone. Based on this answer, I believe DefaultIfEmpty comes into it somewhere, but I can't see how I apply this to x_PersonPhone and then continue to join on that table for the matching rows.