Linq 左连接 left join
Suppose you have a tblRoom and tblUserInfo. Now, you need to select all the rooms regardless of whether the room has user information or not. This calls for a LEFT JOIN which will select everything from the LEFT side (the room side) regardless of the join on the right side. Here is the example.
假如你有两张表tblRoom(房 间表)和tblUserInfo(住户表)。
现在你需要检索出所有房间的信息,而不管这个房间是否有人居住。这就需要进行LEFT JOIN(左外连接),左外连接会检索出LEFT JOIN左边表中的所有行,而不管右边的表是否有匹配项。下面是一个例子:
var list = from r in dc.tblRooms
join ui in dc.tblUserInfos
on r.UserName equals ui.UserNameinto userrooms from ur in userrooms.DefaultIfEmpty() select new
{
FirstName = (ur.FirstName == null) ? "N/A" : ur.FirstName,
LastName = (ur.LastName == null) ? "N/A" : ur.LastName,
RoomName = r.Name
};
he anonymous type replaces the "null" FirstName and LastName with "N/A" (not available).
使用"N/A"(不可得)代替 FirstName 和 LastName 值为"null"的情况。
另附:Linq实现多个表 LEFT JOIN 如下
目标SQL语句(多表 LEFT JOIN 查询)
SELECT id, name, jname, cname
FROM userinfo u
LEFT JOIN job j on u.job = j.jid
LEFT JOIN city c on u.city = c.cid
Linq To Sql 实现三个表 LEFT JOIN 如下:
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();
3个表 LEFT JOIN 例子:
Emp(员工表)、Dept(部门表)、KqEmp(人员考勤信息表)

Linq 左连接 left join的更多相关文章
- GroupBy分组的运用和linq左连接
最简单的分组 var conHistoryList = conHistoryData.GroupBy(g => g.personId); 就是conHistoryData是一个IQueryabl ...
- 数据库左连接left join、右连接right join、内连接inner join on 及 where条件查询的区别
join on 与 where 条件的执行先后顺序: join on 条件先执行,where条件后执行:join on的条件在连接表时过滤,而where则是在生成中间表后对临时表过滤 left joi ...
- 左连接LEFT JOIN 连接自己时的查询结果测试
#左连接LEFT JOIN 连接自己时的查询结果测试 #左连接LEFT JOIN 连接自己时的查询结果(都会出现两个重复字段),两个表都有as后只能查询相等条件merchant_shop_id非nul ...
- EF Linq中的左连接Left Join查询
linq中的join是inner join内连接,就是当两个表中有一个表对应的数据没有的时候那个关联就不成立. 比如表A B的数据如下 from a in A join b in B on a.BId ...
- Linq Left Join;linq左连接 (转载)
来源 https://www.cnblogs.com/xinjian/archive/2010/11/17/1879959.html 准备一些测试数据,如下: use Test Create tabl ...
- 数据库中的左连接(left join)和右连接(right join)区别
Left Join / Right Join /inner join相关 关于左连接和右连接总结性的一句话: 左连接where只影向右表,右连接where只影响左表. Left Join select ...
- Oracle 左连接 left join、右连接right join说明
Oracle 左.右连接 + 在等号 左边表示右连接 获取右表所有记录,即使左表没有对应匹配的记录. + 在等号 右边表示左连接 获取左表所有记录,即使右表没有对应匹配的记录. 例子: selec ...
- C# linq左连接与分组
1.左连接使用DefaultIfEmpty(): 2.分组时候判断newper.FirstOrDefault() == null ? null: newper.ToList()这个经常出错误,如果不判 ...
- linq 左连接后实现与主表一对一关系数据
var query1 = from r in _residentRepository.GetAll() join i in _inLogRepository.GetAll() on r.Id equa ...
随机推荐
- 升级3.4成3.6 ubuntu14.04 和miniconda虚拟环境
打开Anaconda Prompt窗口conda update conda 先升级conda激活要升级python的虚拟环境conda install python=3.6.6 再升级pythonco ...
- 用xshell-ssh连接服务器被经常意外中断
xshell突然中断报错 Socket error Event: 32 Error: 10053.Connection closing...Socket close. Connection close ...
- shell中字体变色
在linux中给字体使用数字代码变色 字体颜色代码:重置0 ,黑色30,红色31,绿色32,黄色33,蓝色34,洋红35,青色36,浅灰37 效果代码:1m加粗 2m加下划线 5m闪动效果 7m ...
- (转)Oracle中动态SQL详解
本文转载自:http://www.cnblogs.com/gaolonglong/archive/2011/05/31/2064790.html 1.静态SQLSQL与动态SQL Oracle编译PL ...
- 好用的一个object c 宏
好用的一个object c 宏 from https://github.com/justzt/ios-helper/blob/master/Macro.h // // Macro.h // Photo ...
- Linux下的Memcache安装,启动
一.linux安装memcache 1. 如果通过下载源码进行安装,则需要下载最新版本http://memcached.googlecode.com/files/memcached-1.4.13.ta ...
- PL/SQL 训练07--发现问题
drop table ma_schedue_task ; ---test_task(:1,:2) create table ma_schedue_task( created_by ) default ...
- git的分布式和集中式
当然,Git的优势不单是不必联网这么简单,后面我们还会看到Git极其强大的分支管理,把SVN等远远抛在了后面.
- C++获取Lua全局变量和执行Lua多参数多返回值函数
C++代码: // LuaAndC.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #i ...
- CentOS yum 安装RabbitMQ
最近在做机器学习的任务系统,任务模块使用了消息对联,比较快速的搭建方法: 1.安装erlang 下载rpm仓库:wget http://packages.erlang-solutions.com/er ...