我正在尝试通过查询(例如使用实体框架)来搜索匹配的人:
var ret = from s in Lab_SubSpaces
join w1 in Workers on s.Alt_Contact equals w1.WWID
where SqlMethods.Like(w1.Full_Name, search) || SqlMethods.Like(w1.IDSID, search)
select s.Lab_Space_Id;
我现在的问题是我还要搜索5列。所以我认为这意味着我还需要再有6个LEFT加入。我真的不知道如何做到这一点,因为联接不断扩展,而在使用linq的左侧联接中我迷路了。
这是我最终需要复制的SQL调用:
SELECT DISTINCT Lab_Space_Id
FROM Lab_SubSpace
LEFT JOIN Worker AS w1 ON Alt_Contact = w1.WWID
LEFT JOIN Worker AS w2 ON Capital_Contact = w2.WWID
LEFT JOIN Worker AS w3 ON PPHW_Contact = w3.WWID
LEFT JOIN Worker AS w4 ON IT_Contact = w4.WWID
LEFT JOIN Worker AS w5 ON Main_Contact = w5.WWID
LEFT JOIN Worker AS w6 ON Safety_Contact = w6.WWID
WHERE w1.Full_Name LIKE @search OR w1.IDSID LIKE @search OR
w2.Full_Name LIKE @search OR w2.IDSID LIKE @search OR
w3.Full_Name LIKE @search OR w3.IDSID LIKE @search OR
w4.Full_Name LIKE @search OR w4.IDSID LIKE @search OR
w5.Full_Name LIKE @search OR w5.IDSID LIKE @search OR
w6.Full_Name LIKE @search OR w6.IDSID LIKE @search
答案 0 :(得分:0)
好的,我知道了。正如Gert指出的那样,如果没有建立关系,则没有必要,但我仍在为那些只是偶然使用Linq-to-SQL的人而张贴。
var byContactName = from s in context.Lab_SubSpace.AsNoTracking()
from w1 in context.Worker.Where(w => w.WWID == s.Alt_Contact).DefaultIfEmpty()
from w2 in context.Worker.Where(w => w.WWID == s.Main_Contact).DefaultIfEmpty()
from w3 in context.Worker.Where(w => w.WWID == s.IT_Contact).DefaultIfEmpty()
from w4 in context.Worker.Where(w => w.WWID == s.PPHW_Contact).DefaultIfEmpty()
from w5 in context.Worker.Where(w => w.WWID == s.Capital_Contact).DefaultIfEmpty()
from w6 in context.Worker.Where(w => w.WWID == s.Technical_Contact).DefaultIfEmpty()
where w1.Full_Name.ToLower().Contains(text) || w1.IDSID.ToLower().Contains(text) ||
w2.Full_Name.ToLower().Contains(text) || w2.IDSID.ToLower().Contains(text) ||
w3.Full_Name.ToLower().Contains(text) || w3.IDSID.ToLower().Contains(text) ||
w4.Full_Name.ToLower().Contains(text) || w4.IDSID.ToLower().Contains(text) ||
w5.Full_Name.ToLower().Contains(text) || w5.IDSID.ToLower().Contains(text) ||
w6.Full_Name.ToLower().Contains(text) || w6.IDSID.ToLower().Contains(text)
select s.Lab_Space_Id;