Click here to Skip to main content
15,884,298 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:
SQL
SELECT
	*
FROM Person
JOIN Company
	ON Person.CompanyName = Company.CompanyName
	OR
	(
		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):
VB.NET
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.
Posted

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.
VB.NET
' Get all companies in one LINQ query.
Dim combinedCompanies =
	From P in context.Person
	From C in context.Company
	Where
		C.CompanyName Equals P.CompanyName
		And P.CompanyName <> "Unemployed"
		Or
		(
			P.CompanyName = "Unemployed" And
			C.VolunteerJob = True
		)


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

' Join with volunteer companies.
Dim volunteerCompanies =
	From P In context.Person
	From C In context.Company
	Where
		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
 
v3
Comments
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.
Hi,

try using LINQ pad from here[^]

OR is written as || in LINQ

also check this[^]
 
Share this answer
 
v2
Comments
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