LINQ 学习路程 -- 查询操作 Join
Join操作是将两个集合联合
Joining Operators | Usage |
---|---|
Join | 将两个序列连接并返回结果集 |
GroupJoin |
根据key将两个序列连接返回,像是SQL中的Left Join |
Join操作两个集合,inner collection 和 outer collection
它返回一个集合(包含两个集合根据特定条件结合的所有元素),和SQL中的inner join一样
public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, TInner, TResult> resultSelector); public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, TInner, TResult> resultSelector,
IEqualityComparer<TKey> comparer);
IList<string> strList1 = new List<string>() {
"One",
"Two",
"Three",
"Four"
}; IList<string> strList2 = new List<string>() {
"One",
"Two",
"Five",
"Six"
}; var innerJoin = strList1.Join(strList2,
str1 => str1,
str2 => str2,
(str1, str2) => str1);
public class Student{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int StandardID { get; set; }
} public class Standard{
public int StandardID { get; set; }
public string StandardName { get; set; }
}
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John", StandardID = },
new Student() { StudentID = , StudentName = "Moin", StandardID = },
new Student() { StudentID = , StudentName = "Bill", StandardID = },
new Student() { StudentID = , StudentName = "Ram" , StandardID = },
new Student() { StudentID = , StudentName = "Ron" }
}; IList<Standard> standardList = new List<Standard>() {
new Standard(){ StandardID = , StandardName="Standard 1"},
new Standard(){ StandardID = , StandardName="Standard 2"},
new Standard(){ StandardID = , StandardName="Standard 3"}
}; var innerJoin = studentList.Join(// outer sequence
standardList, // inner sequence
student => student.StandardID, // outerKeySelector
standard => standard.StandardID, // innerKeySelector
(student, standard) => new // result selector
{
StudentName = student.StudentName,
StandardName = standard.StandardName
});
在上面的例子中,studentList是外部序列,因为先从它开始查询,Join的第一个参数是指定的内部序列,第二个和第三个参数指定对应的字段
外部序列的key选择器student => student.StandardID表明studentList中每个元素的StandardId字段必须与内部序列standard => standard.StandardID相对应
如果所有的key值相等则被包含在结果集中
最后一个参数是构建结果数据
查询语法
查询语法中的join和方法语法有些不同,它需要外部序列、内部序列、key选择器和结果选择器
from ... in outerSequence join ... in innerSequence on outerKey equals innerKey select ...
GroupJoin
GroupJoin和Join是一样的除了GroupJoin返回一个Group(根据特定的group key)
GroupJoin根据key联合两个序列并根据key分组
public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, IEnumerable<TInner>, TResult> resultSelector); public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, IEnumerable<TInner>,
TResult> resultSelector,
IEqualityComparer<TKey> comparer);
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John", StandardID = },
new Student() { StudentID = , StudentName = "Moin", StandardID = },
new Student() { StudentID = , StudentName = "Bill", StandardID = },
new Student() { StudentID = , StudentName = "Ram", StandardID = },
new Student() { StudentID = , StudentName = "Ron" }
}; IList<Standard> standardList = new List<Standard>() {
new Standard(){ StandardID = , StandardName="Standard 1"},
new Standard(){ StandardID = , StandardName="Standard 2"},
new Standard(){ StandardID = , StandardName="Standard 3"}
}; var groupJoin = standardList.GroupJoin(studentList, //inner sequence
std => std.StandardID, //outerKeySelector
s => s.StandardID, //innerKeySelector
(std, studentsGroup) => new // resultSelector
{
Students = studentsGroup,
StandarFulldName = std.StandardName
}); foreach (var item in groupJoin)
{
Console.WriteLine(item.StandarFulldName ); foreach(var stud in item.Students)
Console.WriteLine(stud.StudentName);
}
查询语法
from ... in outerSequence join ... in innerSequence on outerKey equals innerKey into groupedCollection select ...
LINQ 学习路程 -- 查询操作 Join的更多相关文章
- LINQ 学习路程 -- 查询操作 Expression Tree
表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...
- LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending
Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 Then ...
- LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行
延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...
- LINQ 学习路程 -- 查询操作 where
1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...
- LINQ 学习路程 -- 查询操作 GroupBy ToLookUp
Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...
- LINQ 学习路程 -- 查询操作 let into关键字
IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...
- LINQ 学习路程 -- 查询操作 Aggregate
聚合操作执行数学的运算,如平均数.合计.总数.最大值.最小值 Method Description Aggregate 在集合上执行自定义聚集操作 Average 求平均数 Count 求集合的总数 ...
- LINQ 学习路程 -- 查询操作 Select, SelectMany
IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...
- LINQ 学习路程 -- 查询操作 OfType
OfType操作根据集合中的元素是否是给定的类型进行筛选 IList mixedList = new ArrayList(); mixedList.Add(); mixedList.Add(" ...
随机推荐
- Windows 清除系统垃圾文件
@echo off echo 正在清除系统垃圾文件,请稍等...... del /f /s /q %systemdrive%\*.tmp del /f /s /q %systemdrive%\*._m ...
- 网络相关系列之四:数据解析之SAX方式解析XML数据
一.XML和Json数据的引入: 通常情况下.每一个须要訪问网络的应用程序都会有一个自己的server.我们能够向server提交数据,也能够从server获取数据.只是这个时候就有一个问题,这些数据 ...
- ansible的异步执行
ansible任务的异步执行 96 茶客furu声 关注 2016.07.12 01:40* 字数 458 阅读 1777评论 0喜欢 4 ansible方便在于能批量下发,并返回结果和呈现.简单.高 ...
- android中实现毛笔效果(View 中画图)
近期有一个项目设计一个APP实现通过触摸屏实现毛笔写字效果.传统的绘画板程序直接通过Path的moveTo和LineTo便可实现简单的线条绘画程序.然而要达到毛笔的笔锋效果则须要更为具体点的设计.我的 ...
- thinkPHP5.0的学习研究【序言】
2017年6月19日13:19:151.ThinkPHP V5.0——为API开发而设计的高性能框架2.ThinkPHP是一个免费开源的,快速.简单的面向对象的轻量级PHP开发框架,是为了敏捷WEB应 ...
- 【BZOJ】2186 沙拉公主的困惑
一道很有价值的题. [解析1]欧几里德算法求乘法逆元,前缀和 [Analysis]O(T n log n). [Sum] ①int运算.假设会超出界,第一个数前要加上(LL)即类型转换. ②gcd不变 ...
- 【BZOJ1226】[SDOI2009]学校食堂Dining 状压DP
[BZOJ1226][SDOI2009]学校食堂Dining Description 小F 的学校在城市的一个偏僻角落,所有学生都只好在学校吃饭.学校有一个食堂,虽然简陋,但食堂大厨总能做出让同学们满 ...
- 爬虫入门【3】BeautifulSoup4用法简介
快速开始使用BeautifulSoup 首先创建一个我们需要解析的html文档,这里采用官方文档里面的内容: html_doc = """ <html>< ...
- Biorhythms
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 135099 Accepted: 43146 Description So ...
- Gaby Ivanushka(快排)
Gaby Ivanushka Once upon a time there lived a tsar that has a daughter — Beautiful Vasilisa. There w ...