Linq Left Join;linq左连接 (转载)
来源 https://www.cnblogs.com/xinjian/archive/2010/11/17/1879959.html
准备一些测试数据,如下:
use Test
Create table Student(
ID int identity(1,1) primary key,
[Name] nvarchar(50) not null
) Create Table Book(
ID int identity(1,1) primary key,
[Name] nvarchar(50)not null,
StudentID int not null
) insert into Student values('张三')
insert into Student values('李四')
insert into Student values('王五')
select * from student --张三借的书
insert into Book values('红楼',1)
insert into Book values('大话红楼',1) --李四借的书
insert into Book values('三国',2) --王五没借书 --一本错误的记录
insert into Book values('错误时怎样练成的',111) --左连接
select s.name,b.name from student as s
left join Book as b on s.id=b.studentid --右连接
select s.name,b.name from student as s
right join Book as b on s.id=b.studentid
要用Linq实现左连接,写法如下
DataClasses1DataContext db = new DataClasses1DataContext();
var leftJoinSql = from student in db.Student
join book in db.Book on student.ID equals book.StudentID into temp
from tt in temp.DefaultIfEmpty()
select new
{
sname= student.Name,
bname = tt==null?"":tt.Name//这里主要第二个集合有可能为空。需要判断
};
用Linq实现右连接,写法如下
DataClasses1DataContext db=new DataClasses1DataContext();
var rightJoinSql = from book in db.Book
join stu in db.Student on book.StudentID equals stu.ID into joinTemp
from tmp in joinTemp.DefaultIfEmpty()
select new {
sname=tmp==null?"":tmp.Name,
bname=book.Name };
三表左连接
匿名类
var list = (
from u in dc.userinfos
join j in dc.jobs on u.job equals j.jid into j_join
from x in j_join.DefaultIfEmpty()
join c in dc.cities on u.city equals c.cid into c_join
from v in c_join.DefaultIfEmpty()
select new
{
id = u.id,
name = u.name,
jname = x.jname,
cname = v.cname,
/*u1=u,x1=x,v1=v*/
//不要用对象的方式 因为对象可能为null那么对象.属性就会抛异常
}
).ToList(); for (var i = ; i < list.Count(); i++)
{
Console.WriteLine(list[i].name + '\t' + list[i].jname + '\t' + list[i].cname); //字段为null不报异常
//Console.WriteLine(list[i].u1.name+'\t'+list[i].x1.jname+'\t'+list[i].v1.cname+"\r\n"); //对象x1 v1 有可能为null 抛异常
}
Console.ReadLine();
模型类
linq join 左连接 leftjoin 多个on条件 where 条件
var haveChange = from newScore in newScoreList
join oldScore in oldScoreList
on
new{newScore.ExamId,newScore.StudentId,newScore.Subject,newScore.ClassId} equals
new{oldScore.ExamId,oldScore.StudentId,oldScore.Subject,oldScore.ClassId}
where newScore.Score != oldScore.Score
select newScore;

var query= from cc in COPTC
join cd in COPTD
on new {cc.TC001,cc.TC002} equals new{ cd.TD001,cd.TD002}
into m
from n in m.DefaultIfEmpty()
join ma in MOCTA
on new{cd.TD001, cd.TD002,cd.TD003 } equals new{ ma.TA026,ma.TA027,ma.TA028} where new char[]{'Y','y'}.Contains(ma.TA013)
&& new char[]{'Y','y'}.Contains(cc.TC027)
&& Convert.ToDateTime(ma.TA003) > Convert.ToDateTime("2010-07-20")
&& Convert.ToDateTime(ma.TA003) < Convert.ToDateTime("2010-12-31")
select new
{
cc,
cd
};

参考资料:
https://www.cnblogs.com/weixing/p/4447927.html
https://www.cnblogs.com/joeylee/p/3711654.html
http://developer.51cto.com/art/200909/152189.htm
http://hi.baidu.com/thinsoft/blog/item/83fb1e9089cc7186a877a4b1.html
http://apps.hi.baidu.com/share/detail/12540006
http://www.winu.cn/space-14160-do-blog-id-25172.html
Linq Left Join;linq左连接 (转载)的更多相关文章
- Linq 和 SQL的左连接、右连接、内链接
在我们工作中表连接是很常用的,但常用的有这三种连接方式:左连接.右连接.内链接 在本章节中讲的是1.如何在Linq中使用左连接,右连接,内连接. 2.三种连接之间的特点在哪? 3.Linq的三种连接语 ...
- sql语句中的join连接(左连接、右连接、全连接、内连接)
内部连接(inner join): select * from d_user a inner join D_ORGANIZATION b on a.COMPANY_XID=b.ID 内部链接也是排他 ...
- sql左连接,右连接,内连接
1.sql查询时什么叫左连接和右连接 左连接和右连接都是外部连接,也就是区别于内部连接,它对不满足连接条件的行并不是象内部连接一样将数据完全过滤掉,而是保留一部分数据,行数不会减少. 左或 ...
- 【MySQL】MySQL内连接,左连接,右连接查询
概念 INNER JOIN(内连接):获取两个表中字段匹配关系的记录.也就是只会返回共有的内容. LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录. RIGHT JOIN(右 ...
- EF Linq中的左连接Left Join查询
linq中的join是inner join内连接,就是当两个表中有一个表对应的数据没有的时候那个关联就不成立. 比如表A B的数据如下 from a in A join b in B on a.BId ...
- Linq 左连接 left join
Suppose you have a tblRoom and tblUserInfo. Now, you need to select all the rooms regardless of whet ...
- LINQ的左连接、右连接、内连接和Lamda表达式实现Left join
1.左连接: var LeftJoin = from t1 in l1join t2 in l2on t1.ID equals t2.ID into Joinedt12from t3 in Joine ...
- Linq连接查询之左连接、右连接、内连接、全连接、交叉连接、Union合并、Concat连接、Intersect相交、Except与非查询
内连接查询 内连接与SqL中inner join一样,即找出两个序列的交集 Model1Container model = new Model1Container(); //内连接 var query ...
- Linq实现左连接、右连接
--一本错误的记录 insert into Book values('错误时怎样练成的',111) --左连接 select s.name,b.name from student as s lef ...
随机推荐
- springMVC 数据模型相关注解 可注释类型 ModelAttribute SessionAttributes InitBinder
ModelAttribute 参数/方法SessionAttributes 类InitBinder 方法
- 嵌入式开发之内核内存异常排查---关闭oom killer
通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解.uptimedmesg | tailvmstat 1mpstat -P ALL 1pidstat 1iostat -xz 1free ...
- Java学习-053-JSON工具类演示
日常开发工作,经常遇到使用JSON数据,好久没有写了,今晚抽了三个小时时间写了个JSON工具类,主要包含如下几个方法: 获取JSON字符串所有键路径列表: 获取JSON字符串所有键值列表: JSON数 ...
- CSAGAN的几大重点 - 2
1.生成器 1)MRU(SketchyGAN) 计算过程为: 与DCGAN[46]和ResNet生成架构的定性和定量比较可以在5.3节中找到.MRU块有两个输入:输入特征图xi和图像I,输出特征图yi ...
- 利用c# 多屏显示
公司搞了一个电视墙,要显示不同内容,于是买了一个多接口显卡(现在看来这个方案不是太好,但非常省钱) 要打开的就是几个网页,但要自己手工拖到不同电视上,非常麻烦 于是查了一下资料,发现可以用代码实现,说 ...
- Java 8之Map新增方法<转>
在Java 8中的Map.Entry接口中增加了comparingByKey, comparingByValue方法,它们都返回Comparator<Map.Entry<K,V>&g ...
- 做JAVA的需要了解的框架
spring netty Elasticsearch Eureka Hystrix 接口的依赖性管理 Zuul Config Bus ActiveMQ redis zookper quartz had ...
- js:如何在iframe重载前执行特定动作
问题说明: 点击左侧菜单时,右侧页面中的iframe加载菜单内容,在iframe加载的页面A中使用了websocket.点击其它菜单时,无法主动关闭websocket, 可能会造成websocket链 ...
- SonarQube - 安装与运行SonarQube
1 - 下载SonarQube SonarQube有多个版本,其中CE(Community Edition)版本免费开源,其余的开发者版本.企业版本和数据中心版本都是收费版本. 官网下载:https: ...
- odoo controller 继承
方式一: 继承基类,直接重写方法 from odoo.addons.web.controllers.main import Export class PsExport(Export): @http.r ...