MXS&Vincene  ─╄OvЁ  &0000022─╄OvЁ  MXS&Vincene


MXS&Vincene  ─╄OvЁ:今天很残酷,明天更残酷,后天很美好,但是绝大部分人是死在明天晚上,只有那些真正的英雄才能见到后天的太阳。


MXS&Vincene  ─╄OvЁ:We're here to put a dent in the universe. Otherwise why else even be here?



正文>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Linq中连接主要有组连接、内连接、左外连接、交叉连接四种。各个用法如下。

    注:本文内容主要来自《Linq实战》,本例中用到的对象请见文章底部。 

1、 组连接

    组连接是与分组查询是一样的。即根据分组得到结果。 如下例,根据publisther分组得到结果。

    使用组连接的查询语句如下:

            //使用组连接
var GroupQuery = from publisher in SampleData.Publishers
join book in SampleData.Books
on publisher equals book.Publisher into publisherBooks
select new
{
PublisherName = publisher.Name,
Books = publisherBooks
}; 与上边等同的GroupBy语句如下: //使用Group
var QueryByGroup = from book in SampleData.Books
group book by book.Publisher into grouping
select new
{
PublisherName = grouping.Key.Name,
Books = grouping
}; 2、内连接 内连接与SqL中inner join一样,即找出两个序列的交集。如下例找出book中的Publisher存在于SampleData.Publishers的资料。 内连接查询语句如下: //join查询语句
var joinQuery = from publisher in SampleData.Publishers
join book in SampleData.Books
on publisher equals book.Publisher
select new
{
PublisherName = publisher.Name,
BookName = book.Title
}; 与上边等同的查询操作符语句如下: //join操作符语句
SampleData.Publishers.Join(
SampleData.Books, //join 对象
publisher => publisher, //外部的key
book => book.Publisher, //内部的key
(publisher, book) => new //结果
{
PublisherName = publisher.Name,
BookName = book.Title
}); 3、左外连接 左外连接与SqL中left join一样。如下例找出根据publisher中找出SampleData.Publishers中所有资料和book中存在于publisher的资料。 左外连接查询语句如下: //left join, 为空时用default
var leftJoinQuerybyDefault = from publisher in SampleData.Publishers
join book in SampleData.Books
on publisher equals book.Publisher into publisherBooks
from book in publisherBooks.DefaultIfEmpty()
select new
{
PublisherName = publisher.Name,
BookName = (book == default(Book)) ? "no book" : book.Title
}; 注:上例中使用了DefaultIfEmpty操作符,它能够为实序列提供一个默认的元素。DefaultIfEmpty使用了泛型中的default关键字。default关键字对于引用类型将返回null,而对于值类型则返回0。对于结构体类型,则会根据其成员类型将它们相应地初始化为null(引用类型)或0(值类型)。 我们可以不使用default关键字,但在要DefaultIfEmpty中给定当空时的默认对象值。语句如下: //left join, 为空时使用默认对象
var leftJoinQuery = from publisher in SampleData.Publishers
join book in SampleData.Books
on publisher equals book.Publisher into publisherBooks
from book in publisherBooks.DefaultIfEmpty(
new Book { Title = "" } //设置为空时的默认值
)
select new
{
PublisherName = publisher.Name,
BookName = book.Title
}; 4、交叉连接 交叉连接与SqL中Cross join一样。如下例中找出SampleData.Publishers与SampleData.Books的交叉连接。 交叉连接查询语句: var crossJoinQuery = from publisher in SampleData.Publishers
from book in SampleData.Books
select new
{
PublisherName = publisher.Name,
BookName = book.Title
}; 查询操作符语句: //不使用查询表达式
SampleData.Publishers.SelectMany(publisher => SampleData.Books.Select(
book => new
{
PublisherName = publisher.Name,
BookName = book.Title
}
)); 本像用到的对象: static public class SampleData
{
static public Publisher[] Publishers =
{
new Publisher {Name="FunBooks"},
new Publisher {Name="Joe Publishing"},
new Publisher {Name="I Publisher"}
}; static public Author[] Authors =
{
new Author {FirstName="Johnny", LastName="Good"},
new Author {FirstName="Graziella", LastName="Simplegame"},
new Author {FirstName="Octavio", LastName="Prince"},
new Author {FirstName="Jeremy", LastName="Legrand"}
}; static public Subject[] Subjects =
{
new Subject {Name="Software development"},
new Subject {Name="Novel"},
new Subject {Name="Science fiction"}
}; static public Book[] Books =
{
new Book {
Title="Funny Stories",
Publisher=Publishers[0],
Authors=new[]{Authors[0], Authors[1]},
PageCount=101,
Price=25.55M,
PublicationDate=new DateTime(2004, 11, 10),
Isbn="0-000-77777-2",
Subject=Subjects[0]
},
new Book {
Title="LINQ rules",
Publisher=Publishers[1],
Authors=new[]{Authors[2]},
PageCount=300,
Price=12M,
PublicationDate=new DateTime(2007, 9, 2),
Isbn="0-111-77777-2",
Subject=Subjects[0]
},
new Book {
Title="C# on Rails",
Publisher=Publishers[1],
Authors=new[]{Authors[2]},
PageCount=256,
Price=35.5M,
PublicationDate=new DateTime(2007, 4, 1),
Isbn="0-222-77777-2",
Subject=Subjects[0]
},
new Book {
Title="All your base are belong to us",
Publisher=Publishers[1],
Authors=new[]{Authors[3]},
PageCount=1205,
Price=35.5M,
PublicationDate=new DateTime(2006, 5, 5),
Isbn="0-333-77777-2",
Subject=Subjects[2]
},
new Book {
Title="Bonjour mon Amour",
Publisher=Publishers[0],
Authors=new[]{Authors[1], Authors[0]},
PageCount=50,
Price=29M,
PublicationDate=new DateTime(1973, 2, 18),
Isbn="2-444-77777-2",
Subject=Subjects[1]
}
};
}

  

Linq join的更多相关文章

  1. 解决Linq Join Group by 时报错:Nullable object must have a value.

    Linq Join Group by 时报Nullable object must have a value. 例如: from s in subject on ch.SubId equals s.S ...

  2. Linq→join中指定多个条件

    还是习惯先撸一段SQL * FROM User_Pic P AND P.Guid = R.UserPicGuid ORDER BY PicSize DESC 然后发现Linq中的join不能多条件.. ...

  3. Linq join on 多条件

    var a = from m in DbContext.Set<T1>() join q in DbContext.Set<T2>() on new { m.ID, Phone ...

  4. Linq join right join left join

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  5. C# LINQ Join两个表连接,关联多个条件的写法

    1.sql语句: select * from Users u join Teachers t on u.UserID==t.TeacherID and u.Name=t.Name 2.linq写法: ...

  6. LINQ查询表达式(4) - LINQ Join联接

    内部联接 按照关系数据库的说法,“内部联接”产生一个结果集,对于该结果集内第一个集合中的每个元素,只要在第二个集合中存在一个匹配元素,该元素就会出现一次. 如果第一个集合中的某个元素没有匹配元素,则它 ...

  7. linq join的lambda写法

    var query = _db.Bank_CommercialOpus .Join(_db.Bank_Opus, s => s.OpusID, Opus => Opus.ID, (s, O ...

  8. linq join多字段

    from VS in m2db.Inf_VehicleSale join RS1 in m2db.His_RecSale on new { VS.vehicleCode, auctionCode=VS ...

  9. linq join用法

    单条件: var query = from person in people join pet in pets on person equals pet.Owner select new { Owne ...

随机推荐

  1. Win7 系统下 Firefox hostadmin插件无法修改Host

    问题:  win 7系统,今天用杀毒软件杀了一下毒,firefox hostAdmin插件无法修改Host了,提示“ write hosts file failed check permissions ...

  2. java实现MD5加密

    mport java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Creat ...

  3. JAVA双向链表

    1.链表是一种重要的数据结构,在程序设计中占有很重要的地位 2.我们可以用类List来实现链表结构,用变量Head.Tail.Length.Pointer来实现表头.存储当前结点的指针时有一定的技 巧 ...

  4. vsphere平台为win系统动态扩展磁盘

    1.关闭win虚拟机 2.在vcenter管理中加大磁盘空间 3.开启win虚拟机(此时磁盘并没有加大) 4.打开cmd命令行: 进入分区管理--->查看磁盘--->选择磁盘---> ...

  5. WordPattern

    Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...

  6. 好吧,CSS3 3D transform变换,不过如此!

    一.写在前面的秋裤 早在去年的去年,我就大肆介绍了2D transform相关内容.看过海贼王的都知道,带D的家伙都不是好惹的,2D我辈尚可以应付,3D的话,呵呵,估计我等早就在千里之外被其霸气震晕了 ...

  7. Oracle索引简单介绍与示例

    索引的三大特性 1索引高度 在SQL检索数据(SELECT)的时候,索引的高度的不同对检索的效率有明显的差别,数据库访问索引需要读取的数据块通常是索引的高度+1个数据块数,也就是说索引的高度越高,访问 ...

  8. 本内容中发现无效字符。处理资源 'file:///C:/Users/XDJ/Desktop/1111/press.xml' 时出错。第 5 行,位置: 11 <author>ƽ

    粘贴到编译器中, 然后在复制出即可.

  9. js弹窗

    常用人JS弹窗,lhgDialog 4.20

  10. Box2D淌坑日记: 关节(Joint)和旋转关节(b2RevoluteJoint)

    关节在Box2D的对象组织结构中,与b2Body(刚体)并列.因此两种对象都是由b2World创建并直接管理. 然而Joint有依赖于b2Body的地方,就是它的销毁:当关节所涉及到的刚体被销毁,关节 ...