linq-to-sql中的右外连接

时间:2017-10-12 15:02:05

标签: c# linq linq-to-sql

我是LINQ的新手,我有查询,我用SQL服务器编写,我想翻译/转换为LINQ.Can任何人都指导我正确的方向? THX

这是我的SQL:

SELECT Contact.Name, Contact.E-Mail, 
       Contact Business Relation.No_
FROM   Contact Business Relation RIGHT OUTER JOIN
          Contact ON Contact Business Relation.Contact No_ = Contact.Company No_
WHERE  (Contact.E-Mail = @mail)


这是我的LINQ(我不确定我做得对吗):

                var query = from cbr in db.Contact_Business_Relation
                           join c in db.Contact
                           on cbr.Contact_No_ equals c.Company_No_ into f
                           from c in f.DefaultIfEmpty()
                           where c.E_Mail == Mail
                           select new
                           {
                               Mail = c.E_Mail,
                               No = cbr.No_
                           };

1 个答案:

答案 0 :(得分:1)

不幸的是,linq没有正确的联接。但是,您可以重新排列查询以使用左连接。

var query = from c in db.Contact
            join cbr in db.Contact_Business_Relation
            on c.Company_No_ equals cbr.Contact_No_ into f
            from cbr in f.DefaultIfEmpty()
            where c.E_Mail == Mail
            select new
            {
              Mail = c.E_Mail,
              No = cbr.No_
            };

Join Clause