本文转自:http://www.cnblogs.com/xinjian/archive/2010/11/17/1879959.html

use Test
Create table Student(
ID int identity(,) primary key,
[Name] nvarchar() not null
) Create Table Book(
ID int identity(,) primary key,
[Name] nvarchar()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('红楼',)
insert into Book values('大话红楼',) --李四借的书
insert into Book values('三国',) --王五没借书 --一本错误的记录
insert into Book values('错误时怎样练成的',) --左连接
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 }; 复制代码 参考资料: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的更多相关文章

  1. Linq 中的 left join

    Linq 中的 left join 表A User: 表B UserType: Linq: from t in UserType join u in User on t.typeId equal u. ...

  2. Linq中使用Left Join

    use Test Create table Student( ID ,) primary key, ) not null ) Create Table Book( ID ,) primary key, ...

  3. Linq中的连接(join)

    http://www.cnblogs.com/scottckt/archive/2010/08/11/1797716.html Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下 ...

  4. LINQ中的连接(join)用法示例

    Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下. 1. 组连接 组连接是与分组查询是一样的.即根据分组得到结果. 如下例,根据publisther分组得到结果. 使用组连接的 ...

  5. Linq中使用Left Join rught join

    准备一些测试数据,如下: use Test Create table Student( ID int identity(1,1) primary key, [Name] nvarchar(50) no ...

  6. Linq中使用Left Join 和 Right Join

    原文地址:http://www.cnblogs.com/xinjian/archive/2010/11/17/1879959.html 准备一些测试数据,如下: use Test Create tab ...

  7. linq中如何在join中指定多个条件

    public ActionResult Edit(int id) { using (DataContext db = new DataContext(ConfigurationManager.Conn ...

  8. Linq中的left join

    left join var custs = from c in db.T_Customer join u in db.Sys_User on c.OwnerId equals u.Id into te ...

  9. Linq To Sql中实现Left Join与Inner Join使用Linq语法与lambda表达式

    当前有两个表,sgroup与sgroupuser,两者通过gKey关联,而sgroup表记录的是组,而sgroupuser记录是组中的用户,因此在sgroupuser中不一定有数据.需要使用Left ...

随机推荐

  1. ZOOM - 简单易用的 jQuery 照片相册插件

    jQuery 最令人印象深刻的应用之一就是对图片的处理,它可以让帮助你在你的项目中加入一些让人惊叹的图片切换效果.ZOOM 是一款全屏效果的 jQuery 图片切换展示插件,支持键盘前后按键切换,支持 ...

  2. JavaScript小例子:复选框全选

    JavaScript小例子:复选框全选 这只是一个小例子,很简单,但是这个功能还是很常用的: 实现后效果如图: JavaScript代码: <script type="text/jav ...

  3. Java 静态语句块、语句块、构造函数执行顺序

    class Parent{ static String name = "hello"; { System.out.println("3 parent block" ...

  4. 【转载】安卓APP架构

    注:本篇博文转载于 http://my.oschina.net/mengshuai/blog/541314?fromerr=z8tDxWUH 本文介绍了文章作者从事了几年android应用的开发,经历 ...

  5. 造成OOM(内存溢出)的几种情况

    数据库Cursor没关.当我们操作完数据库后,一定要调用close()释放资源. 构造Adapter没有使用缓存ContentView. @Override public View getView(i ...

  6. 【即时通讯】即时通讯及XMPP概述及…

    在讲解XMPP前,我们需要先了解什么是即时通讯技术: * 即时通讯技术(IM - InstantMessaging)支持用户在线实时交谈.当一方需要发送消息时,用户必须打开一个窗口,以便让用户与交流对 ...

  7. Swift-Switch穿透

    switch vegetable {        case "celery":            print("Add some raisins and make ...

  8. Android App监听软键盘按键的三种方式与改变软键盘右下角确定键样式

    actionNone : 回车键,按下后光标到下一行actionGo : Go,actionSearch : 放大镜actionSend : SendactionNext : NextactionDo ...

  9. CodeForce Round#49 untitled (Hdu 5339)

    Untitled Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  10. array_filter、array_map、array_walk解释

    /** * array_filter 用回调函数处理数组中的各个元素, * 重点在于过滤(而不是新增)某个元素,当你处理到一个元素时, * 如果返回了false,那么这个元素将会被过滤掉.PS:保持了 ...