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. ie6+7+8等对background-color:rgba(),background-img渐变的兼容

    一,ie8兼容rgba()的解决办法 今天遇到了一个问题,要在一个页面中设置一个半透明的白色div.这个貌似不是难题,只需要给这个div设置如下的属性即可: background: rgba(255, ...

  2. animation与transition

    animation 动画,无法直接决定开始时间 demo1 @-webkit-keyframes demo-animation1{ 0% { -webkit-transform:translate3d ...

  3. Android Service(下)

    转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在上一篇文章中,我们学习了Android Service相关的许多重要 ...

  4. 第四课 Grid Control实验 安装JCH2库并且配置好监听,关键步骤和结果截图

      --从OCM 虚拟机中,克隆了安装oracle之前的状态 配置网络: [root@localhost network-scripts]# ifup ifcfg-eth0 [root@localho ...

  5. poj 3959 Alignment of Code <vector>“字符串”

    Description You are working in a team that writes Incredibly Customizable Programming Codewriter (IC ...

  6. 操作数据表中的记录——SELECT (where表达式、GROUP BY、HAVING、LIMIT)

    原文链接:http://www.ifyao.com/2015/01/26/%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E8%A1%A8%E4%B8%AD%E7%9A%84 ...

  7. Mysql常用命令行大全(转)

    第一招.mysql服务的启动和停止 net stop mysql net start mysql 第二招.登陆mysql 语法如下: mysql -u用户名 -p用户密码 键入命令mysql -uro ...

  8. zend framework 初识

    1. 请求顺序 : index.php --> Bootstrap.php --> IndexController.php 2. 验证顺序 : Bootstrap.php function ...

  9. NFC应用(一)卡应用

    门禁卡.停车卡.公交卡工作于NFC的卡模式,是目前日常生活中接触得最多的NFC应用场合.一张小小的卡片,轻触读卡器使可开门禁锁.进出停车场.支付车资,即快捷方便,又安全,易于管理. 以门禁系统为例,通 ...

  10. [置顶] SPL讲解(6)--Condition篇

    SmartPersistenceLayer 2.0 之 Condition篇 原理        强大的Condition功能是SPL的一个特性,可以使用Condition完成绝大部分的条件定义,使用 ...