Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用左外部联接将sql转换为linq_Linq - Fatal编程技术网

使用左外部联接将sql转换为linq

使用左外部联接将sql转换为linq,linq,Linq,我有一个sql留下了外部连接,我想把它转换成linq。sql是 Select P.Surname, P.Othername H.history from customers P left outer join historyfiles H on H.patID = P.patId and H.category1 = 4 where P.id = 2299 非常感谢。您可以尝试以下方法: var query = from p in db.customers.Where(t =>

我有一个sql留下了外部连接,我想把它转换成linq。sql是

Select P.Surname, P.Othername H.history
from customers P left outer join historyfiles H on H.patID = P.patId 
     and H.category1 = 4 
where P.id = 2299
非常感谢。

您可以尝试以下方法:

var query = from p in db.customers.Where(t => t.id == 2299 )
            join h in db.historyfiles.Where(l => l.category1 == 4 ) on p.patID equals h.patID into gj
            from h in gj.DefaultIfEmpty()
            select new { p.Surname, p.Othername, h.History };

的可能重复项,但此项在联接中有一个附加条件(仅影响联接的正确部分)。一旦您决定回答,如果您还包括一些解释,那将非常好。当然,还要确保你所发布的内容至少能编译。您没有,因为它缺少了问题的关键部分—“into gj”是不够的,需要在gj.DefaultIfEmpty()中的h后面加上
谢谢您的回复我编辑了我的答案@IvanStoev