Click here to Skip to main content
14,271,317 members
Rate this:
Please Sign up or sign in to vote.
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:
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):
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
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
	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:
' 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.
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,

try using LINQ pad from here[^]

OR is written as || in LINQ

also check this[^]
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100