LinqMethod 实现 LeftJoin
LinqMethod 实现 LeftJoin
Intro
有时候我们想实现 leftJoin 但是 Linq 提供的 Join 相当于是 INNER JOIN,于是就打算实现一个 LeftJoin 的扩展。
如果要使用到左连接,你需要使用 DefaultIfEmpty 来在没有join到数据的时候返回默认的集合
Solution
/// <summary>
/// LeftJoin extension
/// </summary>
/// <typeparam name="TOuter">outer</typeparam>
/// <typeparam name="TInner">inner</typeparam>
/// <typeparam name="TKey">TKey</typeparam>
/// <typeparam name="TResult">TResult</typeparam>
/// <param name="outer">outer collection</param>
/// <param name="inner">inner collection</param>
/// <param name="outerKeySelector">outerKeySelector</param>
/// <param name="innerKeySelector">innerKeySelector</param>
/// <param name="resultSelector">resultSelector</param>
/// <returns></returns>
public static IEnumerable<TResult> LeftJoin<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector,
Func<TOuter, TInner, TResult> resultSelector)
{
return outer
.GroupJoin(inner, outerKeySelector, innerKeySelector, (outerObj, inners) => new
{
outerObj,
inners = inners.DefaultIfEmpty()
})
.SelectMany(a => a.inners.Select(innerObj => resultSelector(a.outerObj, innerObj)));
}
Use
使用起来和 Join 差不多,下面来展示一个示例:
var posts = new[] { new { PostId = 1, PostTitle = "12333", }, new { PostId = 2, PostTitle = "12333", }, };
var postTags = new[] { new { PostId = 1, Tag = "HHH" } };
var result = posts.LeftJoin(postTags, p => p.PostId, pt => pt.PostId, (p, pt) => new { p.PostId, p.PostTitle, pt?.Tag }).ToArray();
Console.WriteLine(result.ToJson());
输出:
[{"PostId":1,"PostTitle":"12333","Tag":"HHH"},{"PostId":2,"PostTitle":"12333"}]
Memo
这是使用 LinqMethod 实现 LeftJoin,如果要使用 linq 查询语法实现左连接请参考这篇文章
Reference
LinqMethod 实现 LeftJoin的更多相关文章
- laravel 使用DB 鏈接leftJoin查詢
$product_count = DB::table('fook_platform_orderrefund as a') ->leftJoin('fook_platform_orderinfo ...
- leftJoin鏈錶查詢
//待使用券碼 $code_record_no = DB::table('fook_platform_order as a') ->select('o.code','o.apportion_bi ...
- MySql LeftJoin On 与 Where的差异
[MySql LeftJoin On 与 Where的差异] 存在两张表: 分别插入数据: 下面的语句一与语句二会产生不同的结果: 语句一: 结果: 语句二: 结果: 为什么会存在差异,这和on与wh ...
- 不使用left-join等多表关联查询,只用单表查询和Java程序,简便实现“多表查询”效果
上次我们提到,不使用left-loin关联查询,可能是为了提高效率或者配置缓存,也可以简化一下sql语句的编写.只写单表查询,sql真得太简单了.问题是,查询多个表的数据还是非常需要的. 因此,存在这 ...
- php laravel左连接leftJoin多条where语句
通常情况下我们在做leftjoin连接时需要对不止一个条件进行进行匹配,这时候就需要使用闭包方式,如下: leftjoin('db', function ($join) {···}); leftjoi ...
- jpa多表leftjoin 查询,自定义返回结果
sql代码 自定义返回结果 package com.cdp.ecosaas.coredb.uua.sync.vo; import java.io.Serializable; public class ...
- 减少leftJoin的使用 (转)
作为开发,你是否经常碰到下面需要转换用户ID成用户名称的情况: 可惜你的这些业务表出于最少冗余设计要求,只有UserId,而没有UserName,这时你不得不破坏你一个类封装一个表的美好想法, 在你的 ...
- sql中join与left-join图解区别
select a.* from YG_BRSYK a left join(SELECT DISTINCT SYXH, STUFF((SELECT '.'+MS FROM #lsb where SY ...
- linq join 左连接 leftjoin 多个on条件 where 条件
var haveChange = from newScore in newScoreList join oldScore in oldScoreList on new{newScore.ExamId, ...
随机推荐
- 2019阿里天猫团队Java高级工程师面试题之第二面
2019阿里天猫团队Java高级工程师面试题之第一面 2019阿里天猫团队Java高级工程师面试题之第三面 1.Tomcat的基本架构是什么? https://blog.csdn.net/xlgen1 ...
- Mac--ModuleNotFoundError: No module named 'magic'
关于Mac本报错“ ModuleNotFoundError: No module named 'magic' ” 作为一个python程序员,简单粗暴pip install python-magic ...
- tensorflow-- Dataset创建数据集对象
tf.data模块包含: experimental 模块 Dataset 类 FixedLengthRecordDataset 类 TFRecordDataset 类 TextLineDataset ...
- react使用moment进行日期格式化
导入moment import moment from 'moment'; 使用方式 年月日,时分秒 moment().format('YYYY-MM-DD HH:mm:ss'); // 2019-0 ...
- c++ LeetCode(初级数组篇)十一道算法例题代码详解(一)
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/10940636.html 唉!最近忙着面试找实习,然后都是面试的很多是leetcode的算法题, ...
- ASP.NET Core 2.2 WebApi 系列【七】泛型仓储模式和工作单元
在之前的泛型仓储模式实现中,每个增删改都调用了SaveChanges方法,导致每次更新都提交了事务. 在实际开发过程中,我们经常遇到同时操作多张表数据,那么按照之前的写法,对数据库提交了多次操作,开启 ...
- Java面试基础 -- Linux篇
0.日常命令 cd 目录路径: 跳转到指定目录 mkdir 目录名称:新增目录 pwd: 获取当前路径 su: 切换用户 ll: 查看当前目录侠的文件和目录 rm -rf 目录: 递归删除指定目录下的 ...
- Python只读取文本中文字符
#coding=utf-8 import re with open('aaa.txt','r',encoding="utf-8") as f: #data = f.read().d ...
- Data Management Technology(1) -- Introduction
1.Database concepts (1)Data & Information Information Is any kind of event that affects the stat ...
- String类型中 "=="和"equals"比较的差别
String类型中 "=="和"equals"比较的差别 先说明一下String类型的变量的创建方式 在创建新的String类型的变量时,首先会在缓冲区查找是否 ...