linq 之左连接
List<ArticleModel> articleList = articleRepository.GetAllArticle();
List<UsersModel> userList = usersRepository.GetAllUsers();
//用户表左连接文章表
var usersLeftJoin = (from u in userList
join a in articleList
on u.Id equals a.Author into users
from us in users.DefaultIfEmpty()
select new
{
author = us == null ? "" : us.Author.ToString(),
name = u.Name.Trim()
}).ToList();
//要避免这种写法,这种写法查出来的数据实际上相当于连接
var usersLeftJoin1 = (from u in userList
join a in articleList
on u.Id equals a.Author into users
from us in users
select new
{
author = us.Author,
name = u.Name.Trim()
}).ToList();
//文章表左连接用户表
var articleLeftJoin = (from a in articleList
join u in userList
on a.Author equals u.Id into users
from us in users.DefaultIfEmpty()
select new
{
title = a.Title.Trim(),
name = us == null ? "" : us.Name.Trim(),
author = a.Author
}).ToList();
//要避免这种写法,这种写法查出来的数据实际上相当于连接
var articleLeftJoin1 = (from a in articleList
join u in userList
on a.Author equals u.Id into users
from us in users
select new
{
name = us.Name.Trim(),
author = a.Author
}).ToList();
//用户表连接文章表
var result = (from a in articleList
from u in userList
where a.Author == u.Id
select new
{
name = u.Name,
author = a.Author
}).ToList();
linq 之左连接的更多相关文章
- Linq实现左连接、右连接
--一本错误的记录 insert into Book values('错误时怎样练成的',111) --左连接 select s.name,b.name from student as s lef ...
- Lambda方式左连接有Linq方式左连接
网上查到的直接使用Join+DefaultIfEmpty的方式是错误的,实际生成SQL是两表先内联接,然后再LEFT JOIN.经过查证,参考资料,最终得到如下两种方式的左连接写法: public v ...
- LINQ的左连接、右连接、内连接
.左连接: var LeftJoin = from emp in ListOfEmployees join dept in ListOfDepartment on emp.DeptID equals ...
- linq实现左连接
1.左连接: var LeftJoin = from emp in ListOfEmployees join dept in ListOfDepartment on emp.DeptID equals ...
- 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的左连接右连接内连接用法
1.左连接: var LeftJoin = from e in ListOfEmployees join d in ListOfDepartment on e.DeptID equals d.ID i ...
- linq之左连接 + group by
var list = from item in (from s in _sysBll.GetList(s => s.ParamID == "TraSchType" & ...
- linq join 左连接 leftjoin 多个on条件 where 条件
var haveChange = from newScore in newScoreList join oldScore in oldScoreList on new{newScore.ExamId, ...
- Linq Left Join;linq左连接 (转载)
来源 https://www.cnblogs.com/xinjian/archive/2010/11/17/1879959.html 准备一些测试数据,如下: use Test Create tabl ...
随机推荐
- android 开发 获取各种intent (图片、apk文件、excel、pdf等文件)
public static Intent openFile(String filePath){ File file = new File(filePath); if(!file.exists()) r ...
- DevExpress GridControl使用(转)
DevExpress GridControl使用 (一)原汁原味的表格展示 Dev控件中的表格控件GridControl控件非常强大.不过,一些细枝末节的地方有时候用起来不好找挺讨厌的.使用过程中,多 ...
- SVN提交错误:working copy is not up-to-date解决方法
我在项目中删了2个jar,然后SVN提交,一直提交不成功 svn在提交时报错如下图: working copy is not up-to-date svn:commit failed(details ...
- C# 获取属性字段上DescriptionAttribute的值
var ent = new Ent(); foreach (var item in ent.GetType().GetProperties()) { var v = (DescriptionAttri ...
- 解决Navicat Error: Missing required libmysql_d.dll
在Navicat(H:\Program Files (x86)\Navicat for MySQL)目录下找到libmysql_d.dll,复制到C盘:system/wow64文件夹下. 重新打开na ...
- uva 1056
floyd 算法 用了stl 的map 存名字的时候比较方便 #include <cstdio> #include <cstdlib> #include <cmath&g ...
- 解决unity3d发布的网页游戏放到服务器上无法使用的问题
http://www.unity蛮牛.com/blog-2429-1226.html 第一次把unity3d发布的网页游戏放到服务器上(Win2003),发现无法使用.可以尝试以下办法. ...
- C#委托及事件
转载:http://www.cnblogs.com/warensoft/archive/2010/03/19/1689806.html C#委托及事件 在C#中,委托(delegate)是一种引用类型 ...
- poj 3358 Period of an Infinite Binary Expansion
由乘2取整得到分数的小数位,可以找到规律!!! 例如:1/10,2/10,4/10,8/10,16/10,32/10,64/10…… 取整后:1/10,2/10,4/10,8/10,6/10,2/10 ...
- hdu 1370 Biorhythms
中国剩余定理……. 链接http://acm.hdu.edu.cn/showproblem.php?pid=1370 /**************************************** ...