Join子句据说可以实现3中连接关系。

1.内部连接——元素的连接关系必须同时满足被连接的两个数据源

2.分组连接

3.左外连接

1.最基本的,内部连接,类似于sql中inner join。

由于student类和phone类只有一个匹配的连接,因此,只返回一个id,1.

 public class student
{
public int id;
string name;
public student(int id, string name)
{
this.id = id;
this.name = name;
}
}
public class phone
{
public int id;
string number;
public phone(int i, string number)
{
this.id = i;
this.number = number;
} } class Program
{
static void Main(string[] args)
{
List<student> stu=new List<student>();
List<phone> ph=new List<phone>();
student st1=new student (,"bob");
student st2 = new student(, "tony");
phone p1 = new phone(, "");
stu.Add(st1);
stu.Add(st2);
ph.Add(p1);
var result = from x in ph
join s in stu on x.id equals s.id
select x;
foreach(var r in result)
Console.WriteLine(r.id);
Console.ReadLine();
}
}

2.分组连接 类似于full join

注意其中ToList的用法。我还不很明白,Mark之。

     public class student
{
public int id;
public string name;
public student(int id, string name)
{
this.id = id;
this.name = name;
}
}
public class phone
{
public int id;
public string number;
public phone(int i, string number)
{
this.id = i;
this.number = number;
} } class Program
{
static void Main(string[] args)
{
List<student> stu=new List<student>();
List<phone> ph=new List<phone>();
student st1=new student (,"bob");
student st2 = new student(, "tony");
phone p1 = new phone(, "");
stu.Add(st1);
stu.Add(st2);
ph.Add(p1);
var result = from s in stu
join p in ph on s.id equals p.id into g
select new
{
id = s.id,
name = s.name,
phone = g.ToList()
};
foreach (var r in result)
{
Console.WriteLine(r.id);
Console.WriteLine(r.name);
if (r.phone.Count > )
Console.WriteLine(r.phone[].number);
else
Console.WriteLine("xxxx");
}
Console.ReadLine();
}
}

结果如图

3.类似Left join,往往与DefaultIfEmpty()结合使用,若第一个集合中的元素没有找到相关元素,DefaultIfEmpty()可以指定该元素的相关元素的默认元素。

讲phone作为第一个元素

  class Program
{
static void Main(string[] args)
{
List<student> stu=new List<student>();
List<phone> ph=new List<phone>();
student st1=new student (,"bob");
student st2 = new student(, "tony");
phone p1 = new phone(, "");
stu.Add(st1);
stu.Add(st2);
ph.Add(p1);
var result = from p in ph
join s in stu on p.id equals s.id into g
from pc in g.DefaultIfEmpty()
select new
{
id = p.id,
num = p.number,
name = g.ToList()
};
foreach (var r in result)
{
Console.WriteLine(r.id);
Console.WriteLine(r.num);
if (r.name.Count > )
Console.WriteLine(r.name[].name);
else
Console.WriteLine("xxxx");
}
Console.ReadLine();
}

结果如图

通常,若要生成两个集合的左外部连接,可以分两步实现,

其一,使用分组连接执行内部连接。

其二,对分组连接中每个匹配元素序列调用DefaultIfEmpty().

LINQ 基本子句之二 join的更多相关文章

  1. Linq 多表连接查询join

    在查询语言中,通常需要使用联接操作.在 LINQ 中,可以通过 join 子句实现联接操作.join 子句可以将来自不同源序列,并且在对象模型中没有直接关系(数据库表之间没有关系)的元素相关联,唯一的 ...

  2. LINQ TO SQL 中的join(转帖)

    http://www.cnblogs.com/ASPNET2008/archive/2008/12/21/1358152.html join对于喜欢写SQL的朋友来说还是比较实用,也比较容易接受的东西 ...

  3. linq总结系列(二)---Expression

    一.linq中的表达式和表达式树 Linq中的表达式(Expression<TDel>)是强类型的lambda表达式,对Func和Action形式的委托做了一层封装. lambda表达式的 ...

  4. LINQ&EF任我行(二)--LinQ to Object

    (原创:灰灰虫的家http://hi.baidu.com/grayworm)LinQ to Objects是LinQ家庭的核心,其它的LinQ也使用了与LinQ to Objects相同的查询句法.最 ...

  5. LINQ to SQL语句之Join和Order By

    Join操作 适用场景:在我们表关系中有一对一关系,一对多关系,多对多关系等.对各个表之间的关系,就用这些实现对多个表的操作. 说明:在Join操作中,分别为Join(Join查询), SelectM ...

  6. LINQ学习笔记(二)

    上一篇是根据百度百科写的随便,同时也纠正我对LINQ的看法,因为首次接触LINQ是使用EF对数据库数据的操作. 所以误以为它操作数据库的一种新手段. LINQ语言集成查询是一组技术的名称,这些技术建立 ...

  7. LINQ基础 之 LINQ TO SQL (二)

    配置LINQ TO SQL 首先添加一个Linq to sql文件,以.dbml结尾的文件.无法把表拖拽到.dbml文件中,提示“所选对象使用不支持的数据提供程序” 解决方案 在服务器资源管理器中右键 ...

  8. Using LINQ Group By and String.Join() / Aggregate() in Entity Framework 3.5

    linq to sql 的时候,有时候需要用到 先group  然后来个 aggregate 串连一下值, 但会总会出错,说不识别 aggregate 或者 string.join 方法 搜遍网络 一 ...

  9. LINQ,EF联合查询join

    public object GetListAdmin() { //return db_C56.Admins //   .Where(a => a.Status != "D") ...

随机推荐

  1. 89c52串口发送接收小示例

    //串口发送 void sendChar(char *p)//调用前关中断,调用完成后关中断 { while(*p != '\0') { SBUF = *P while(!TI); TI = 0; p ...

  2. zabbix-check of pre-requisites

    LAMP搭建完成后,访问http://ip/zabbix,在检查环境界面,有的检查项目提示fail.常见如下:zabbix:Check of pre-requisites1.PHP bcmath fa ...

  3. 第二章 Android Studio使用第三方模拟器

    1.为什么要使用第三方模拟器 Android Studio自带模拟器,相对Eclipse来说项目启动速度的确快了很多倍,提高了开发效率.但和第三方模拟器进行对比的话,还是第三方的模拟器运行速度更快些. ...

  4. (转) Virtual function

    原文地址:http://en.wikipedia.org/wiki/Virtual_function In object-oriented programming, a virtual functio ...

  5. [转载]启用 VIM 中的 Python 自动补全及提示功能

    转载: http://zhongwei-leg.iteye.com/blog/941474 周围的同事不喜欢使用 VIM 写 Python 代码的原因之一就是,VIM 不能像 Visual Studi ...

  6. YUI之数组操作

    YUI的构建数组,将类数组转换成真正的数组,从而可以使用数组的所有方法   数组构建 //真正的数组返回1,类数组返回2,其余的返回0 YArray.test = function (obj) { v ...

  7. Hibernate中HQL的日期差值计算,可计算相差多少秒

    最近有个业务需求就是计算订单创建时间离现在超过 4 小时的订单都查找出来! 那么就需要用到日期函数了. 网上找了一下总共的日期函数有一下几个: CURRENT_DATE() 返回数据库当前日期 时间函 ...

  8. JavaScript权威指南阅读笔记3

    第六章 对象 1.首先是先介绍了对象直接量的格式:对象直接量就是1.由若干个名/值对组成的映射表,2名/值对中间由冒号分割,3名值对之间由逗号分割,4整个映射表由花括号括起来.这样就组成了一个对象直接 ...

  9. 转:ProGuard 常见命令备份

    转: https://my.oschina.net/sunyh/blog/30359 #-dontshrink #不压缩,减少jar的大小一般都压缩掉,为了增加反编译的难度也可以不压缩 #-donto ...

  10. [C++程序设计]字符数组的赋值与引用

    只能对字符数组的元素赋值,而不能用赋值语句对整个数组赋值. char c[5]; c={′C′,′h′,′i′,′n′,′a′}; //错误,不能对整个数组一次赋值 c[0]=′C′; c[1]=′h ...