Click here to Skip to main content
15,747,908 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
Note: C# or VB.NET answers are fine, but my question will be in VB.NET...

I have been trying to convert the following SQL into a LINQ to Entity statement:
FROM Person
JOIN Company
	ON Person.CompanyName = Company.CompanyName
		Person.CompanyName = 'Unemployed'
		AND Company.VolunteerJob = 1

However, LINQ does not seem to support the OR operator in JOIN clauses. A coworker of mine helpfully suggested I use a UNION, which I will try tomorrow, but I'd like to know if there is a more obvious syntax. To clarify, this is what I am trying to do (the syntax does not support this):
Dim results =
    From Person In context.Person
    Join Company In context.Company
        On Person.CompanyName Equals Company.CompanyName Or
        (Person.CompanyName Equals "Unemployed" And Company.VolunteerJob = 1)

Note that the tables/fields I am really using are nothing like the above... I am just using them above for demonstration purposes.

There are two approaches I am considering, but I have to look into the performance before I decide. One approach puts all the conditions in the WHERE clause, and the other approach UNIONs two initial result sets.
' Get all companies in one LINQ query.
Dim combinedCompanies =
	From P in context.Person
	From C in context.Company
		C.CompanyName Equals P.CompanyName
		And P.CompanyName <> "Unemployed"
			P.CompanyName = "Unemployed" And
			C.VolunteerJob = True

This is the version that my coworker recommended:
' Join with normal companies.
Dim matchingCompanies =
	From P In context.Person
	Join C In context.Company
		On C.CompanyName Equals P.CompanyName
		P.CompanyName <> "Unemployed"

' Join with volunteer companies.
Dim volunteerCompanies =
	From P In context.Person
	From C In context.Company
		P.CompanyName = "Unemployed" And
		C.VolunteerJob = True

' Combine normal and volunteer companies.
Dim combinedCompanies = matchingCompanies.Union(volunteerCompanies)

I like the brevity of the first approach, but I'm not sure it will be as performant as the second approach.
Share this answer
AspDotNetDev 12-Jul-11 14:58pm    
To anybody reading this, I went with the first option. It takes less than 40 milliseconds to run on some huge tables. I am not sure if the second option works.

try using LINQ pad from here[^]

OR is written as || in LINQ

also check this[^]
Share this answer
AspDotNetDev 12-Jul-11 11:11am    
FYI, I voted you a 2 because this is not an answer. || in C# is the same as OR in VB.NET, which doesn't help me much because that operator apparently can't be used in a JOIN. I already have LINQPad, yet I don't magically have an answer from it. Was there something specific at the link you sent me to that has an answer, or is it just another general LINQ resource?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900