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. wlan的QOS配置

    WLAN QoS配置 1.1  WLAN QoS简介 802.11网络提供了基于竞争的无线接入服务,但是不同的应用需求对于网络的要求是不同的,而原始的网络不能为不同的应用提供不同质量的接入服务,所以已 ...

  2. Unity的Profiler性能分析

    1. CPU Usage A. WaitForTargetFPS: Vsync(垂直同步)功能所,即显示当前帧的CPU等待时间 B. Overhead: Profiler总体时间-所有单项的记录时间总 ...

  3. 两个在线编写C++代码的网站

    第一个支持的编程语言很多,首推 http://ideone.com/ 第二个简单了点,不过可以通过email分享给朋友 http://codepad.org/ 试一试,可以用这两个网站提高自己编写代码 ...

  4. xp下删除windows7,无法删除windows7文件夹,无法删除windows7文件,双系统卸载,取得文件权限

    http://blog.csdn.net/lanmanck/article/details/5722050 ---------------------------------------------- ...

  5. delphi 程序是否为控制台编译选项

    http://www.birdol.com/article/tag/delphi 编译指令写在哪?: 编译指令可以写在代码页的任何地方, 不过在代码的不同区域有时也会不同; 譬如: {$APPTYPE ...

  6. 具有 Button 风格的 Panel(覆盖TCustomPanel的Paint函数,用到了ThemeServices)

    unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  7. 【POJ】1035 Spell checker

    字典树. #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib ...

  8. 【转】BLE 学习记录

    原文网址:http://m.blog.csdn.net/blog/chiooo/43985401 BLE 学习记录 ANROID BLE 开发,基于 bluetoothlegatt 分析 mBluet ...

  9. Android设备信息、感应器检测

    近日产品已经上线,开始有时间来做自己的事情,于是就开始学习和巩固一些以前用过的或者学过的技术.昨天写了一个检测Android设备的序列号和IMEI以及感应器等等的一个Demo来跟大家分享一下. 在开发 ...

  10. Java---类反射(1)---类反射入门和基础

    什么是类反射 ☆什么是反射 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方 ...