c# - SQL to LINQ - 左连接两个条件
问题描述
如何将此外部左连接转换为 LINQ?
SELECT * from table1 t1
left join table2 t2 on t1.code = t2.code AND t2.id ='xxxxx'
WHERE t1.someId = 2
我正在尝试这样的事情:
from t1 in db.table1
join t2 in db.table2 on t1.Code equals t2.Code
into oj
from sub in oj.DefaultIfEmpty()
where t1.someId == 2
where (sub.id == 'xxxx')
但并非左表中的所有行都被返回。我认为 where 子句在加入后应用。
解决方案
var res=(from t1 in table1.Where(x=>x.someId==2)
join t2 in table2.Where(y=>y.id==xxxx)
on t1.code = t2.code
into r
from t3 in r.DefaultIfEmpty()
select new {t1,t3}).ToList();
推荐阅读
- python - 如何使用 pygame.transform.scale 调整图像大小?
- python - GUI 单位转换米/英尺
- javascript - Django、html和js中的匹配卡片
- r - 如何在R中并行读取和写入csv文件
- node.js - 将 dockerized Nginx 上的 React App 连接到 Express 服务器
- reactjs - 不确定我的道具在反应原生打字稿中是什么类型
- c++ - 如何在 C++ 中的 switch 语句之后编写 if 语句?
- python - numpy如何避免gc引用计数对子进程的访问复制
- javascript - 如何让圆半径随滑块输入而变化?
- arrays - 长度超过 255 个字符的数组公式。减少长度但得到输出#NA