Linq left join 怎么写??? 求正解!

Jueyoung 2010-08-12 02:00:00
要求 分别实现类似以下两种情况的 Linq 语句

谢谢!

select * 
from table1
left join table2 on table1.Id = table2.empId
left join table3 on table2.deptId = table3.deptId
where table1.id > 10



select * 
from table1
left join table2 on table1.Id = table2.empId
left join table3 on table1.deptId = table3.deptId
where table1.id > 10


...全文
788 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jueyoung 2010-08-12
  • 打赏
  • 举报
回复
LINQ to Entities does not recognize the method , and this method cannot be translated into a store expression.

为什么啊???
Jueyoung 2010-08-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 q107770540 的回复:]

C# code

//1 .
DbContext db=new DbContext ();
var q=from t1 in db.Table1
join t2 in db.Table2
on t1.Id equals t2.empId
join t3 in db.Table3
……
[/Quote]


这样貌似就是 inner join 了呀
Jueyoung 2010-08-12
  • 打赏
  • 举报
回复
谢谢各位 我要逐一再试一试
wuyq11 2010-08-12
  • 打赏
  • 举报
回复
var q =
from e in db.Employees
join o in db.Books on e.EmployeeID equals o.EmployeeID into b
from x in b.DefaultIfEmpty()
select new
{
e.ID,
e.Name,
o.BookName
};
q107770540 2010-08-12
  • 打赏
  • 举报
回复

//1 .
DbContext db=new DbContext ();
var q=from t1 in db.Table1
join t2 in db.Table2
on t1.Id equals t2.empId
join t3 in db.Table3
on t2.deptId equals t3.detpId
where t1.id>10
select new {t1.id,t1......}


//2

DbContext db=new DbContext ();
var q=from t1 in db.Table1
join t2 in db.Table2
on t1.Id equals t2.empId
join t3 in db.Table3
on t1.deptId equals t3.detpId
where t1.id>10
select new {t1.id,t1......}

q107770540 2010-08-12
  • 打赏
  • 举报
回复
使用DefaultIfEmpty 翻译成SQL语句不是LEFT JOIN,而是LEFT OUT JOIN
醉爱 2010-08-12
  • 打赏
  • 举报
回复
你先看看tb里面有没有值
Jueyoung 2010-08-12
  • 打赏
  • 举报
回复
但是这样写 我用 tb.FirstOrDefault() 就出错呢?
醉爱 2010-08-12
  • 打赏
  • 举报
回复
1)
var tb = from tb1 in table1
join tb2 in table2 on tb1.Id equals tb2.empId into ggg
from gg in ggg.DefaultIfEmpty()
join tb3 in table3 on gg.deptId equals tb3.deptId into kkk
from kk in kkk.DefaultIfEmpty()
where (tb1.Id > 10)
select new
{
tb1.id
...
kk.deptId
...
};
2)
var tb = from tb1 in table1
join tb2 in table2 on tb1.Id equals tb2.empId into ggg
from gg in ggg.DefaultIfEmpty()
join tb3 in table3 on tb1.deptId equals tb3.deptId into kkk
from kk in kkk.DefaultIfEmpty()
where (tb1.Id > 10)
select new
{
tb1.id
...
kk.deptId
...
};
Jueyoung 2010-08-12
  • 打赏
  • 举报
回复
么有人呢?
内容概要:本文围绕“【SCUC】N-1故障集+安全约束机组组合研究(Matlab代码实现)”展开,系统探讨电力系统在N-1故障场景下的安全约束机组组合(SCUC)问题,旨在实现系统安全性与运行经济性的协同优化。研究通过构建包含机组启停、出力计划、潮流约束、备用容量及N-1安全准则的数学模型,结合混合整数线性规划等优化算法,利用Matlab平台完成仿真求解,并引入YALMIP等建模工具提升建模效率。文档提供了完整的代码实现方案与测试案例,涵盖故障集的生成与处理方法,帮助用户掌握鲁棒性和分布鲁棒优化的基本思想,适用于电力系统安全校核与调度决策支持。; 适合人群:电力系统及相关专业的研究生、从事电力调度、能源优化的科研人员及工程技术人员。; 使用场景及目标:① 掌握N-1安全准则在机组组合中的建模方法与实现流程;② 开展安全约束机组组合(SCUC)与安全约束经济调度(SCED)的仿真分析;③ 利用Matlab/YALMIP快速搭建电力系统优化模型并进行求解与验证。; 阅读建议:建议读者结合文档内容与网盘提供的完整资源(含代码与数据),动手实践建模与调试过程,重点关注约束条件的设定、故障场景的枚举与处理策略,同时参考相关文献深化对鲁棒优化与分布鲁棒方法的理解。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧