惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
F
Fortinet All Blogs
J
Java Code Geeks
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
M
MIT News - Artificial intelligence
腾讯CDC
Last Week in AI
Last Week in AI
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
博客园 - 叶小钗
Recent Announcements
Recent Announcements
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
人人都是产品经理
人人都是产品经理
L
LangChain Blog
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - 特洛伊-Micro

Anatomy of a .PPTX File: Unpacking the Open XML Format fiddlerscriptCustomize Menus Fiddler And LINQ Fiddler 过滤 js、css、jpg 等请求 Open in Windows Terminal as administrator .NET 内存碎片化分析 Android 常见脱壳与反编译工具 使用夜神模拟器调试安卓apk C#实现前向最大匹配、字典树(分词、检索) Task.Run vs Task.Factory.StartNew 时下最流行的5个用于桌面应用程序开发的 JS 框架介绍 What Is A Web Application Firewall? Ubuntu18.04 source.list更新国内源 ubuntu16.04 wordpress建站教程 Ubuntu1604 Install PHP 7.4 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) Inside the Storage Engine: Anatomy of a record Inside the Storage Engine: Anatomy of a page MSSQL forensics (1) - MDF fundamentals
Linq 中 Join 的用法
特洛伊-Micro · 2025-07-24 · via 博客园 - 特洛伊-Micro

Linq 中 Join 的用法

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]
      }
    };
  }

复制代码

转载于:http://www.cnblogs.com/scottckt/archive/2010/08/11/1797716.html