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)) ? "" : 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
}
));

5.Linq实现左连接,写法如下

 var leftJoinSql = from student in db.Student
join book in db.Book on student.ID equals book.StudentID into temp
from tt in temp.DefaultIfEmpty()
select new
{
sname= student.Name,
bname = tt==null?"":tt.Name//这里主要第二个集合有可能为空。需要判断
};

6.Linq实现右连接,写法如下

 var rightJoinSql = from book in db.Book
join stu in db.Student on book.StudentID equals stu.ID into joinTemp
from tmp in joinTemp.DefaultIfEmpty()
select new {
sname=tmp==null?"":tmp.Name,
bname=book.Name };

LINQ中的连接(join)用法示例的更多相关文章

  1. Linq中的连接(join)

    http://www.cnblogs.com/scottckt/archive/2010/08/11/1797716.html Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下 ...

  2. Linq 中的 left join

    Linq 中的 left join 表A User: 表B UserType: Linq: from t in UserType join u in User on t.typeId equal u. ...

  3. Linq中使用Left Join

    use Test Create table Student( ID ,) primary key, ) not null ) Create Table Book( ID ,) primary key, ...

  4. [转]Linq中使用Left Join

    本文转自:http://www.cnblogs.com/xinjian/archive/2010/11/17/1879959.html use Test Create table Student( I ...

  5. T-SQL 中的CROSS JOIN用法(半翻译)

    突然发现个很吊的链接,我们来看看学习数据库要做些什么,胆小慎点:DBA工作内容!!!! 今天来翻译一篇关于T-SQL的文章,本文可供微软认证70-461:QueryingMicrosoft SQL S ...

  6. Linq中使用Left Join rught join

    准备一些测试数据,如下: use Test Create table Student( ID int identity(1,1) primary key, [Name] nvarchar(50) no ...

  7. Linq中使用Left Join 和 Right Join

    原文地址:http://www.cnblogs.com/xinjian/archive/2010/11/17/1879959.html 准备一些测试数据,如下: use Test Create tab ...

  8. shell中字典的一个用法示例

    1. shell中字典的用法示例, 数组用法同字典 2. 以上使用sell配合字典实现的功能, 使用awk能轻松搞定, 如下: awk '{print $2}' file.txt | sort | u ...

  9. C#中this指针的用法示例

    这篇文章主要介绍了C#中this指针的用法,对初学者而言是非常重要的概念,必须加以熟练掌握,需要的朋友可以参考下. 本文实例展示了C#中this指针的用法,对于初学者进一步牢固掌握C#有很大帮助,具体 ...

随机推荐

  1. 【转】【Asp.Net】ASP.Net Response.ContentType 详细列表

    不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式. 代码如: <% response.ContentType =&quo ...

  2. e557. 在Applet中显示图片

    See also e551 精简的Applet. Image image; public void init() { // Load image image = getImage(getDocumen ...

  3. PHP 去除iphone,ios,emoji表情

    public static function removeEmoji($text) { $clean_text = ""; // Match Emoticons $regexEmo ...

  4. 【C++基础 05】友元函数和友元类

    友元是一种定义在类外部的普通函数或类,但它须要在类体内进行说明,为了与该类的成员函数加以差别,在说明时前面加以keywordfriend. 友元不是成员函数,可是它能够訪问类中的私有成员. 友元的作用 ...

  5. fildder教程

    转载地址:写得很不错的fildder教程   http://kb.cnblogs.com/page/130367/ Fiddler的基本介绍 Fiddler的官方网站:  www.fiddler2.c ...

  6. Ini操作类

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  7. mybatis之parameterType传递多个参数

    当在查询的时候需要传入多个参数的时候该怎么办呢: 1,封装成一个Model对象,底层HashMap还是一个 User user=new User(); user.setUserName("z ...

  8. APP的缓存文件放在哪里?

    只要是需要进行联网获取数据的APP,都会在本地产生缓存文件.那么,这些缓存文件到底放在什地方合适呢?系统有没有给我们提供建议的缓存位置呢?不同的缓存位置有什么不同呢? 考虑到卸载APP必须删除缓存 在 ...

  9. mqtt 服务器与客户端通讯

    mqtt 服务器与客户端通讯. 服务器端 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...

  10. NUC970 U-Boot 使用說明

    U-Boot 使用說明U-Boot 是一個主要用於嵌入式系統的開機載入程式, 可以支援多種不同的計算機系統結構, 包括ARM.MIPS.x86與 68K. 這也是一套在GNU通用公共許可證之下發布的自 ...