求Linq写法

兔子家族-二哥 2017-08-23 05:52:10
例如有学生表A 借书信息表B,图书基础表C 图书高级详情表D 假设一个学生只能借一本书 现在要查询全校学生所借书得列表,基础信息,高级信息

那一般我的做法会:
from a in A from b in B where A与B得关系
select new { 学生名称,基础信息=from c in C where C 与B/A得关系 select new
{基础信息。。。。,高级信息=from d in D where D与C得关系 select D}}

这种写法出来 C和D 转为JSON就是个数组,能否不用这种写法
因为是一对一 一个学生一本书,能不能把基础信息放到和 学生名称同级?思路上都觉得可以,但是第一个梗linq new得时候集合会让你加别名,需求是不能加。
如果把from C 放到 from AB 同级是可用解决层级问题,但是 没有借书得学生就查询不出来,如题要查询所有学生。
Sql中子查询就可解决,那Linq中如何做这个需求?
...全文
306 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
peng2739956 2017-08-25
  • 打赏
  • 举报
回复
引用 14 楼 God_Girl 的回复:
[quote=引用 12 楼 peng2739956 的回复:] 不能做了left join 嘛 非得 条件限定也一起、?

var list = from   e1 in LinqDBContext.wbCheckInfo 
                        join  a in LinqDBContext.wbCheckList on a.wbCheckId equals e1.wbCheckId
                           join  b in LinqDBContext.site on a.siteid equals b.siteid
                          join c in LinqDBContext.onlineFiresystem on b.siteid equals c.siteid
                           join  d in LinqDBContext.firesystype on c.tiSysType equals d.tiSysType
                          select new {//to do..}
                           
这样难道不行吗
本来 前面前面4张表是内联,后面第五张是依据前4张得查询结果做 左连接得。你得这种写法也报错无法将表达式转换为 SQL 并且无法将它视为局部表达式 [/quote] 如果是这样的话,你可以 2张表 2张表连接查询,看问题出现在第几张表上面。
  • 打赏
  • 举报
回复
引用 12 楼 peng2739956 的回复:
不能做了left join 嘛 非得 条件限定也一起、?

var list = from   e1 in LinqDBContext.wbCheckInfo 
                        join  a in LinqDBContext.wbCheckList on a.wbCheckId equals e1.wbCheckId
                           join  b in LinqDBContext.site on a.siteid equals b.siteid
                          join c in LinqDBContext.onlineFiresystem on b.siteid equals c.siteid
                           join  d in LinqDBContext.firesystype on c.tiSysType equals d.tiSysType
                          select new {//to do..}
                           
这样难道不行吗
本来 前面前面4张表是内联,后面第五张是依据前4张得查询结果做 左连接得。你得这种写法也报错无法将表达式转换为 SQL 并且无法将它视为局部表达式
  • 打赏
  • 举报
回复
引用 12 楼 peng2739956 的回复:
不能做了left join 嘛 非得 条件限定也一起、?

var list = from   e1 in LinqDBContext.wbCheckInfo 
                        join  a in LinqDBContext.wbCheckList on a.wbCheckId equals e1.wbCheckId
                           join  b in LinqDBContext.site on a.siteid equals b.siteid
                          join c in LinqDBContext.onlineFiresystem on b.siteid equals c.siteid
                           join  d in LinqDBContext.firesystype on c.tiSysType equals d.tiSysType
                          select new {//to do..}
                           
这样难道不行吗
倒也不是不行,我试试吧
peng2739956 2017-08-25
  • 打赏
  • 举报
回复
不能做了left join 嘛 非得 条件限定也一起、?

var list = from   e1 in LinqDBContext.wbCheckInfo 
                        join  a in LinqDBContext.wbCheckList on a.wbCheckId equals e1.wbCheckId
                           join  b in LinqDBContext.site on a.siteid equals b.siteid
                          join c in LinqDBContext.onlineFiresystem on b.siteid equals c.siteid
                           join  d in LinqDBContext.firesystype on c.tiSysType equals d.tiSysType
                          select new {//to do..}
                           
这样难道不行吗
  • 打赏
  • 举报
回复
引用 9 楼 duanzi_peng 的回复:
[quote=引用 8 楼 God_Girl 的回复:] [quote=引用 5 楼 duanzi_peng 的回复:] 但是 没有借书得学生就查询不出来,如题要查询所有学生 -》 已“学生表A 借书信息表B” 为例:
from q in A
from p in B.Where(w => w.stuId == q.Id).DefaultIfEmpty()
select new 
{
   q.Id,
   q.Name,
   p.stuId,
   p.bName,
}
wbCheckId=string.IsNullOrEmpty(e1.wbCheckId)? "":e1.wbCheckId, 列里面要加判断才行,不然就GG了,行吧 斑竹去那边 也回个帖子 给你结账了[/quote] 噢,是么。只要记着 DefaultIfEmpty 用在右侧表就行,至于条件问题还得自己解决。问题解决了就好,分不分的无所谓。[/quote] 斑竹 求救还好没结贴 不然GGl了。 我现在报错 var list = from a in LinqDBContext.wbCheckList from b in LinqDBContext.site from c in LinqDBContext.onlineFiresystem from d in LinqDBContext.firesystype from e1 in LinqDBContext.wbCheckInfo.Where(x => x.siteid == c.siteid && x.tiSysType == c.tiSysType && x.wbCheckId == a.wbCheckId).DefaultIfEmpty() where a.siteid == b.siteid && b.siteid == c.siteid && c.tiSysType == d.tiSysType && a.wbCheckId == wbCheckId select new { c.MaintenanceId, d.tiSysType, d.vSysdesc, wbCheckId = string.IsNullOrEmpty(e1.wbCheckId) ? "" : e1.wbCheckId, wbCheckResult = string.IsNullOrEmpty(e1.wbCheckResult) ? "" : e1.wbCheckResult, wbProblemRemaks = string.IsNullOrEmpty(e1.wbProblemRemaks) ? "" : e1.wbProblemRemaks, wbId = string.IsNullOrEmpty(e1.wbId + "") ? "" : e1.wbId + "", YnHanding = string.IsNullOrEmpty(e1.YnHanding) ? "" : e1.YnHanding, Handingimmediately = string.IsNullOrEmpty(e1.Handingimmediately) ? "" : e1.Handingimmediately, c.siteid, PicUrl = from f in LinqDBContext.wbCheckPic where e1.wbCheckId == f.wbCheckId && e1.siteid == f.siteid && e1.tiSysType == f.tiSysType select URL + f.PicUrl, IsEdit= IsEdit }; if (!string.IsNullOrEmpty(MaintenanceId)) { list = list.Where(x => x.MaintenanceId == MaintenanceId); } 无法将表达式转换为 SQL 并且无法将它视为局部表达式 我哪儿错了啊、、、、看起来没错啊
exception92 2017-08-25
  • 打赏
  • 举报
回复
引用 11 楼 God_Girl 的回复:
斑竹 求救还好没结贴 不然GGl了。 我现在报错 var list = from a in LinqDBContext.wbCheckList from b in LinqDBContext.site from c in LinqDBContext.onlineFiresystem from d in LinqDBContext.firesystype from e1 in LinqDBContext.wbCheckInfo.Where(x => x.siteid == c.siteid && x.tiSysType == c.tiSysType && x.wbCheckId == a.wbCheckId).DefaultIfEmpty() where a.siteid == b.siteid && b.siteid == c.siteid && c.tiSysType == d.tiSysType && a.wbCheckId == wbCheckId select new { c.MaintenanceId, d.tiSysType, d.vSysdesc, wbCheckId = string.IsNullOrEmpty(e1.wbCheckId) ? "" : e1.wbCheckId, wbCheckResult = string.IsNullOrEmpty(e1.wbCheckResult) ? "" : e1.wbCheckResult, wbProblemRemaks = string.IsNullOrEmpty(e1.wbProblemRemaks) ? "" : e1.wbProblemRemaks, wbId = string.IsNullOrEmpty(e1.wbId + "") ? "" : e1.wbId + "", YnHanding = string.IsNullOrEmpty(e1.YnHanding) ? "" : e1.YnHanding, Handingimmediately = string.IsNullOrEmpty(e1.Handingimmediately) ? "" : e1.Handingimmediately, c.siteid, PicUrl = from f in LinqDBContext.wbCheckPic where e1.wbCheckId == f.wbCheckId && e1.siteid == f.siteid && e1.tiSysType == f.tiSysType select URL + f.PicUrl, IsEdit= IsEdit }; if (!string.IsNullOrEmpty(MaintenanceId)) { list = list.Where(x => x.MaintenanceId == MaintenanceId); } 无法将表达式转换为 SQL 并且无法将它视为局部表达式 我哪儿错了啊、、、、看起来没错啊
某些linq to entity语句 转换不了可执行的sql语句,你去掉picUrl ,IsEdit 的赋值测试看看
  • 打赏
  • 举报
回复
引用 5 楼 duanzi_peng 的回复:
但是 没有借书得学生就查询不出来,如题要查询所有学生 -》 已“学生表A 借书信息表B” 为例:
from q in A
from p in B.Where(w => w.stuId == q.Id).DefaultIfEmpty()
select new 
{
   q.Id,
   q.Name,
   p.stuId,
   p.bName,
}
wbCheckId=string.IsNullOrEmpty(e1.wbCheckId)? "":e1.wbCheckId, 列里面要加判断才行,不然就GG了,行吧 斑竹去那边 也回个帖子 给你结账了
  • 打赏
  • 举报
回复
引用 5 楼 duanzi_peng 的回复:
但是 没有借书得学生就查询不出来,如题要查询所有学生 -》 已“学生表A 借书信息表B” 为例:
from q in A
from p in B.Where(w => w.stuId == q.Id).DefaultIfEmpty()
select new 
{
   q.Id,
   q.Name,
   p.stuId,
   p.bName,
}
from a in LinqDBContext.wbCheckList from b in LinqDBContext.site from c in LinqDBContext.onlineFiresystem from d in LinqDBContext.firesystype from e1 in LinqDBContext.wbCheckInfo.Where(x=>x.siteid==c.siteid&&x.tiSysType==c.tiSysType&&x.wbCheckId==a.wbCheckId).DefaultIfEmpty() where a.siteid == b.siteid && b.orgid == orgid && b.siteid == c.siteid && c.tiSysType == d.tiSysType
  • 打赏
  • 举报
回复
引用 5 楼 duanzi_peng 的回复:
但是 没有借书得学生就查询不出来,如题要查询所有学生 -》 已“学生表A 借书信息表B” 为例:
from q in A
from p in B.Where(w => w.stuId == q.Id).DefaultIfEmpty()
select new 
{
   q.Id,
   q.Name,
   p.stuId,
   p.bName,
}
还是不行 还是SQL right join得值,查不出来全部得 http://bbs.csdn.net/topics/392237312
exception92 2017-08-24
  • 打赏
  • 举报
回复
但是 没有借书得学生就查询不出来,如题要查询所有学生 -》 已“学生表A 借书信息表B” 为例:
from q in A
from p in B.Where(w => w.stuId == q.Id).DefaultIfEmpty()
select new 
{
   q.Id,
   q.Name,
   p.stuId,
   p.bName,
}
peng2739956 2017-08-24
  • 打赏
  • 举报
回复
你就弄个测试数据出来, 2张表的。 然后你想得到什么信息 贴出来, 不然不好回答。DefaultIfEmpty() 这个是在没有数据的情况下写入NULL 保证能枚举数据。
exception92 2017-08-24
  • 打赏
  • 举报
回复
引用 8 楼 God_Girl 的回复:
[quote=引用 5 楼 duanzi_peng 的回复:] 但是 没有借书得学生就查询不出来,如题要查询所有学生 -》 已“学生表A 借书信息表B” 为例:
from q in A
from p in B.Where(w => w.stuId == q.Id).DefaultIfEmpty()
select new 
{
   q.Id,
   q.Name,
   p.stuId,
   p.bName,
}
wbCheckId=string.IsNullOrEmpty(e1.wbCheckId)? "":e1.wbCheckId, 列里面要加判断才行,不然就GG了,行吧 斑竹去那边 也回个帖子 给你结账了[/quote] 噢,是么。只要记着 DefaultIfEmpty 用在右侧表就行,至于条件问题还得自己解决。问题解决了就好,分不分的无所谓。
  • 打赏
  • 举报
回复
from a in LinqDBContext.wbCheckList from b in LinqDBContext.site from c in LinqDBContext.onlineFiresystem from d in LinqDBContext.firesystype join e in LinqDBContext.wbCheckInfo on new { siteid = c.siteid, wbCheckId = a.wbCheckId, tiSysType = (int?)c.tiSysType } equals new { siteid = e.siteid, wbCheckId = e.wbCheckId, tiSysType = (int?)e.tiSysType } into f1 from fnew in f1.Select(x=>new { x.wbCheckId,x.wbCheckResult,x.wbProblemRemaks,x.wbId,x.YnHanding,x.Handingimmediately }) 一样得,没查询出全部
  • 打赏
  • 举报
回复
引用 1 楼 duanzi_peng 的回复:
查询 Linq DefaultIfEmpty 关键字
from a in LinqDBContext.wbCheckList from b in LinqDBContext.site from c in LinqDBContext.onlineFiresystem from d in LinqDBContext.firesystype join e in LinqDBContext.wbCheckInfo on new { siteid = c.siteid, wbCheckId = a.wbCheckId, tiSysType = c.tiSysType } equals new { siteid = e.siteid, wbCheckId = e.wbCheckId, tiSysType = e.tiSysType } into f from fnew in f.DefaultIfEmpty() where a.siteid == b.siteid && b.orgid == orgid && b.siteid == c.siteid && c.tiSysType == d.tiSysType 报错了,无法将null赋值给 Int类型、、、 tiSysType 为int类型 这个需要怎么处理一下?
  • 打赏
  • 举报
回复
引用 1 楼 duanzi_peng 的回复:
查询 Linq DefaultIfEmpty 关键字
是使用 join 来做么? Join 只能 on 一个查询条件,但是我这边有多个条件啊。 这是我实际得写法: from a in LinqDBContext.wbCheckList from b in LinqDBContext.site from c in LinqDBContext.onlineFiresystem from d in LinqDBContext.firesystype join e in LinqDBContext.wbCheckInfo on a.wbCheckId equals e.wbCheckId into f from fnew in f.DefaultIfEmpty().Where(x=>x.tiSysType== c.tiSysType && x.siteid==a.siteid) where a.siteid == b.siteid && b.orgid == orgid && b.siteid == c.siteid && c.tiSysType == d.tiSysType 查询结果 当 e 为空得a b c d 得记录就没得 就相当于没有查询全部数据
exception92 2017-08-23
  • 打赏
  • 举报
回复
查询 Linq DefaultIfEmpty 关键字

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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