IEnumerable的一些基本方法 补充
接上一篇,我们发现两表连接方式默认为内连接,而我们在SQL中常用到的左连接没有封装方法。换句话说,微软放弃两表左连或右连的这种做法(只有在2个表都存在值时,这样的连接才有意义)。
如果要实现表的左连接,就不能调用他现有的封装方法了,可以用LINQ来实现。
var joinTable = (from left in table1.AsEnumerable()
join right in table2.AsEnumerable()
on left["ID"].ToString() equals right["ID"].ToString() into newTable
from right in newTable.DefaultIfEmpty()
select new
{
LeftID = left["ID"].ToString(),
RightID = right != null ? right["ID"].ToString() : default(string),
LeftName = left["Name"].ToString(),
RightName = right != null ? right["Name"].ToString() : default(string)
}).ToList();
joinTable.ForEach(t => Console.WriteLine("{0}\t{1}\t{2}\t{3}", t.LeftID, t.RightID, t.LeftName, t.RightName));
功能是实现了,但是这样又要憋死一群强迫症患者,非要用Join封装方法来实现这个功能。
那我们就来手动封装一个左连接方法吧。
public static IEnumerable<Result> LeftJoin<TOuter, TInner, TKey, Result>(
this IEnumerable<TOuter> outer
, IEnumerable<TInner> inner
, Func<TOuter, TKey> outerKeySelector
, Func<TInner, TKey> innerKeySelector
, Func<TOuter, TInner, Result> resultSelector
, IEqualityComparer<TKey> comparer)
{
if (outer == null)
throw new ArgumentException("outer"); if (inner == null)
throw new ArgumentException("inner"); if (outerKeySelector == null)
throw new ArgumentException("outerKeySelector"); if (innerKeySelector == null)
throw new ArgumentException("innerKeySelector"); if (resultSelector == null)
throw new ArgumentException("resultSelector"); return LeftJoinImpl(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer ?? EqualityComparer<TKey>.Default);
} static IEnumerable<Result> LeftJoinImpl<TOuter, TInner, TKey, Result>(
IEnumerable<TOuter> outer, IEnumerable<TInner> inner
, Func<TOuter, TKey> outerKeySelector
, Func<TInner, TKey> innerKeySelector
, Func<TOuter, TInner, Result> resultSelector
, IEqualityComparer<TKey> comparer)
{
var innerLookup = inner.ToLookup(innerKeySelector, comparer); foreach (var outerElment in outer)
{
var outerKey = outerKeySelector(outerElment);
var innerElements = innerLookup[outerKey]; if (innerElements.Any())
foreach (var innerElement in innerElements)
yield return resultSelector(outerElment, innerElement);
else
yield return resultSelector(outerElment, default(TInner));
}
}
PS:以上这段代码转载自stackoverflow.com
var joinTable = table1.AsEnumerable().LeftJoin(table2.AsEnumerable(),
left => left["ID"].ToString(),
right => right["ID"].ToString(),
(left, right) => new
{
LeftID = left["ID"].ToString(),
RightID = right != null ? right["ID"].ToString() : default(string),
LeftName = left["Name"].ToString(),
RightName = right != null ? right["Name"].ToString() : default(string)
}, null).ToList();
joinTable.ForEach(t => Console.WriteLine("{0}\t{1}\t{2}\t{3}", t.LeftID, t.RightID, t.LeftName, t.RightName));
是不是和之前的Join方法一样,这样用起来很方便了吧。
IEnumerable的一些基本方法 补充的更多相关文章
- C# 索引器,实现IEnumerable接口的GetEnumerator()方法
当自定义类需要实现索引时,可以在类中实现索引器. 用Table作为例子,Table由多个Row组成,Row由多个Cell组成, 我们需要实现自定义的table[0],row[0] 索引器定义格式为 [ ...
- 为IEnumerable扩展一个ForEach方法
IEnumerable没有一个ForEach方法,我们可以使用C#写一个扩展方法: Source Code: using System; using System.Collections.Generi ...
- IEnumerable接口的扩展方法
/// <summary>/// IEnumerable接口的扩展方法,支持它的实现类是List的情况/// </summary>using System.Collection ...
- python 魔法方法补充(__setattr__,__getattr__,__getattribute__)
python 魔法方法补充 1 getattribute (print(ob.name) -- obj.func())当访问对象的属性或者是方法的时候触发 class F(object): def _ ...
- python基础知识五 各类型数据方法补充,转换,分类,编码+坑中菜
3.9各类型数据方法补充,转换,分类,编码,坑中菜 3.9.1数据类型方法补充 1.str:不可变 补充方法 s1.capitalize():首字母大写 s1 = "alex" s ...
- C#对IQueryable<T>、IEnumerable<T>的扩展方法
#region IQueryable<T>的扩展方法 #region 根据第三方条件是否为真是否执行指定条件的查询 /// <summary> /// 根据第三方条件是否为真是 ...
- 像画笔一样慢慢画出Path的三种方法(补充第四种)
今天大家在群里大家非常热闹的讨论像画笔一样慢慢画出Path的这种效果该如何实现. 北京-LGL 博客号@ligl007发起了这个话题.然后各路高手踊跃发表意见.最后雷叔 上海-雷蒙 博客号@雷蒙之星 ...
- IEnumerable接口的Aggregate方法
以前小猪为了累加一个集合中的类容通常会写出类似这样的C#代码: string result ="": foreach (var item in items) { result+=i ...
- IEnumerable中的 Any方法
IEnumerable类中的 Any方法,表示集合中有任何一元素满足条件,返回就true , 该方法有两个重载 1. 不带任何参数,表示集合中有元素 2. 参入一个 Func<TSource, ...
随机推荐
- JAVA 虚拟机内存
1.普通java应用程序,使用java命令运行, java -Xms1024m -Xmx1024m -XX:MaxNewSize=256m -XX:MaxPermSize=256m -jar 2.t ...
- c/c++基本数据类型大小
各个类型的变量长度由编译器来决定(实际上与操作系统位数和编译器都有关)使用时可用sizeof()得到,当前主流编译器一般是32位或64位. 类型 32位 64位 char 1 1 short int ...
- Linux C 获取系统时间信息
比如获取当前年份: /* 获取当前系统时间 暂时不使用 ; ; time_t now; struct tm *timenow; time(&now); timeno ...
- ES6学习笔记之变量声明let,const
最近用淘宝的weex做了个项目,最近稍微闲下来了.正好很久没有接触RN了,所以趁这个机会系统的学习一下ES6的相关知识. 孔子说:没有对比就没有伤害.所以我们要拿ES6和ES5好好对比的学习.这样才能 ...
- 【BZOJ4403】序列统计 Lucas定理
[BZOJ4403]序列统计 Description 给定三个正整数N.L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量.输出答案对10^6+3取模的结果. Input 输入第 ...
- Digit Division
Digit Division Time limit: 1 s Memory limit: 512 MiB We are given a sequence of n decimal digits. Th ...
- Display BLOBs and CLOBs (DB2可视化工具AQT )
Displaying BLOBs and CLOBs Dealing with LOBs BLOB and CLOB columns are difficult to display because: ...
- 非Linux环境下调用sh命令
方法一:把cygwin的bin配置到环境变量里,这样做了以后在cmd.exe里也可以使用linux的命令 def exe_command(command): p = subprocess.Popen( ...
- 006-搭建框架-实现AOP机制【三】AOP技术
2.3.spring+aspectj Spring在集成了aspectj后,同时也保留了以上的切面与代理的配置方式. 将Spring与aspectj集成与直接使用aspectj不同,不需要定义Aspe ...
- Java并发—线程常用方法总结
1.join()方法 join —— 让一个线程等待另一个线程完成才继续执行.如A线程线程执行体中调用B线程的join()方法,则A线程被阻塞,知道B线程执行完为止,A才能得以继续执行. public ...