且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在JOIN子句中使用OR条件进行LINQ查询

更新时间:2023-10-23 15:25:28

我有两种方法我正在考虑,但在决定之前我必须研究一下性能.一种方法将所有条件放在WHERE子句中,另一种方法UNION将两个初始结果集组合在一起.
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.




尝试从这里使用LINQ pad [ ^ ]

OR写为||在LINQ

还要检查 [
Hi,

try using LINQ pad from here[^]

OR is written as || in LINQ

also check this[^]