using System;
using System.Collections.Generic;
using System.Linq; namespace Linq101
{
internal class Join
{
/// <summary>
/// This sample shows how to efficiently join elements of two sequences based on equality between key expressions over the two.
/// </summary>
public void Linq102()
{
string[] categories = { "Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood" };
List<Data.Product> products = Data.GetProductList(); var q = from c in categories
join p in products on c equals p.Category
select new { Category = c, p.ProductName }; ObjectDumper.Write(q);
} /// <summary>
/// Using a group join you can get all the products that match a given category bundled as a sequence.
/// </summary>
public void Linq103()
{
string[] categories = { "Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood" };
List<Data.Product> products = Data.GetProductList(); var q = from c in categories
join p in products on c equals p.Category into ps
select new { Category = c, Products = ps }; foreach (var v in q)
{
Console.WriteLine(v.Category + ":");
foreach (var p in v.Products)
{
Console.WriteLine(" " + p.ProductName);
}
}
} /// <summary>
/// The group join operator is more general than join, as this slightly more verbose version of the cross join sample shows.
/// </summary>
public void Linq104()
{
string[] categories = { "Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood" };
List<Data.Product> products = Data.GetProductList(); var q = from c in categories
join p in products on c equals p.Category into ps
from p in ps
select new { Category = c, p.ProductName }; foreach (var v in q)
{
Console.WriteLine(v.ProductName + ": " + v.Category);
}
} /// <summary>
/// A so-called outer join can be expressed with a group join. A left outer joinis like a cross join,
/// except that all the left hand side elements get included at least once, even if they don't match any right hand side elements.
/// Note how Vegetablesshows up in the output even though it has no matching products.
/// </summary>
public void Linq105()
{
string[] categories = { "Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood" };
List<Data.Product> products = Data.GetProductList(); var q = from c in categories
join p in products on c equals p.Category into ps
from p in ps.DefaultIfEmpty()
select new { Category = c, ProductName = p == null ? "(No Products)" : p.ProductName }; foreach (var v in q)
{
Console.WriteLine(v.ProductName + ": " + v.Category);
}
}
}
}

Linq101-Join的更多相关文章

  1. SQL Server-聚焦IN VS EXISTS VS JOIN性能分析(十九)

    前言 本节我们开始讲讲这一系列性能比较的终极篇IN VS EXISTS VS JOIN的性能分析,前面系列有人一直在说场景不够,这里我们结合查询索引列.非索引列.查询小表.查询大表来综合分析,简短的内 ...

  2. SQL Server-聚焦NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL性能分析(十八)

    前言 本节我们来综合比较NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL的性能,简短的内容,深入的理解,Always to review the basics. ...

  3. Nested Loops join时显示no join predicate原因分析以及解决办法

    本文出处:http://www.cnblogs.com/wy123/p/6238844.html 最近遇到一个存储过程在某些特殊的情况下,效率极其低效, 至于底下到什么程度我现在都没有一个确切的数据, ...

  4. c# Enumerable中Aggregate和Join的使用

    参考页面: http://www.yuanjiaocheng.net/ASPNET-CORE/asp.net-core-environment.html http://www.yuanjiaochen ...

  5. 超详细mysql left join,right join,inner join用法分析

    下面是例子分析表A记录如下: aID        aNum 1           a20050111 2           a20050112 3           a20050113 4   ...

  6. join Linq

    List<Publisher> Publishers = new List<Publisher>(); Publisher publish1 = new Publisher() ...

  7. mysql join 和left join 对于索引的问题

    今天遇到一个left join优化的问题,搞了一下午,中间查了不少资料,对MySQL的查询计划还有查询优化有了更进一步的了解,做一个简单的记录: select c.* from hotel_info_ ...

  8. BCL中String.Join的实现

    在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的对象转换为“1,2,3”这样的 ...

  9. [数据库基础]——图解JOIN

    阅读导航 一.概要 二.JOIN分类 三.JOIN分类详解 一.概要 JOIN对于接触过数据库的人,这个词都不陌生,而且很多人很清楚各种JOIN,还有很多人对这个理解也不是很透彻,这次就说说JOIN操 ...

  10. Spark join 源码跟读记录

    PairRDDFunctions类提供了以下两个join接口,只提供一个参数,不指定分区函数时默认使用HashPartitioner;提供numPartitions参数时,其内部的分区函数是HashP ...

随机推荐

  1. 1. android

    http://blog.csdn.net/mirkerson/article/details/7238815

  2. Unity3d 合作开发项目

    Unity3d  合作开发项目    交流群:63438968  本人:灰太龙 项目的合作开发是至关重要的,第一个问题就是自适应分辨率的问题! 综合考虑了一下,我们采用了IGUI插件,这个插件有以下几 ...

  3. hdu 5135 Little Zu Chongzhi's Triangles

    http://acm.hdu.edu.cn/showproblem.php?pid=5135 题意:给你N个木棍的长度,然后让你组成三角形,问你组成的三角形的和最大是多少? 思路:先求出可以组成的所有 ...

  4. 要开始深入VMM了。

    得到一个VMM机器所有的节点状态 Quick one-liner to generate a CSV of virtual machines, sorted by their hosts. Repor ...

  5. 深入浅出 Java Concurrency (2): 原子操作 part 1

    转:http://www.blogjava.net/xylz/archive/2010/07/01/324988.html 从相对简单的Atomic入手(java.util.concurrent是基于 ...

  6. Maven实战五

    转载:http://www.iteye.com/topic/1123232 我们项目中用到的jar包可以通过依赖的方式引入,构建项目的时候从Maven仓库下载即可. 1. 依赖配置    依赖可以声明 ...

  7. sphinx插入代码

    示例的Python源代码或者交互界面都可以使用标准reST模块实现.在正常段落后面跟着 :: 开始,再加上适当缩进. 交互界面需包含提示及Python代码的输出. 交互界面没有特别的标记. 在最后一行 ...

  8. COJ 0047 20702最大乘积

    20702最大乘积 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 输入n个元素组成的序列s,你需要找出一个乘积最大的连续子序列 ...

  9. 【转】寻找最好的笔记软件:三强篇(EverNote、Mybase、Surfulater) (v1.0) (

    原文网址:http://blog.sina.com.cn/s/blog_46dac66f01000b57.html 寻找最好的笔记软件:三强篇(EverNote.Mybase.Surfulater) ...

  10. 动态规划(二维背包问题):UVAoj 473

     Raucous Rockers  You just inherited the rights to n previously unreleased songs recorded by the pop ...