这是生成一组联系人的名字,姓氏和电子邮件地址的脚本。
SELECT DISTINCT pvld.AllTeam, rc.FirstName, rc.LastName, rc.EmailAddress
FROM CustomerPreferredVendorDetail pvld
INNER JOIN Teams t ON t.TeamId = pvld.TeamId AND t.Active = 1
INNER JOIN TeamMembers tm ON
(tm.TeamMemberId = pvld.TeamMemberId AND (pvld.AllTeam = 0 OR pvld.AllTeam IS NULL))
OR
(tm.TeamId = t.TeamId AND pvld.AllTeam = 1)
AND tm.Active = 1
INNER JOIN ResourceContact rc ON rc.SupplierContactsID = tm.ResourceContactId
AND rc.Active = 1
WHERE pvld.CustCompId = 4383
我想写一个linq查询,但我坚持加入团队和 TeamMembers 表的条件。如果AllTeam字段为false或null,那么我需要使用TeamMemberId加入 TeamMembers 表。否则,我将使用 TeamId 加入。
这就是我开始编写查询的方式:
var contactInfos = from pvld in _repository.GetQuery<CustomerPreferredVendorDetail>
(p => p.CustCompId == CustCompId)
join tm in _repository.GetAll<Team>()
on pvld.TeamId equals tm.TeamId
join tmbr in _repository.GetAll<TeamMember>()
on pvld.TeamMemberId equals () //.. this is where I'm stuck...
看起来连接只能用原始值完成,而不是复杂的条件。
有没有办法将上述SQL查询写入linq?或者我需要多次调用才能获得相同的结果?