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. php开发入门教程

    LAMP window:WAMP(windows,apache,mysql,php) LAMP是 Linux,Apache,MySQL和PHP的缩写,是我们提供 Web 服务的软件基础. 对于 Lin ...

  2. 【VB】操作ODBC-DAO方式操作只能查询,不能更新插入操作解决。

    最近接手一个改善项目,需要从Access转化到SQL Server 2014,使用原有的ODBC连接方式只能查询,不能更新插入.网上一直找不到解决方案,然后自己测试一下使用ADO方式竟然可以连接了.具 ...

  3. temp 临时文件

    放假了,放假了:http://blog.csdn.net/skywalker_only/article/details/17076851 nucht2.2.1爆出如下异常: Exception in ...

  4. Unity3D 3D横版跑酷

    Unity3d  3D横版跑酷系列(Character Controller组件) @广州小龙 目前在做一个3D跑酷的横版游戏,目前说一下 Character Controller组件! 1.Slop ...

  5. unity3d中的http通信

    转载 http://blog.csdn.net/mfc11/article/details/8188785的博客,如果侵权,请留言我及时删除! 前言 Unity3d 是一个跨平台的引擎,在移动互联网浪 ...

  6. Linux Kernel 本地拒绝服务漏洞

    漏洞名称: Linux Kernel 本地拒绝服务漏洞 CNNVD编号: CNNVD-201308-090 发布时间: 2013-08-08 更新时间: 2013-08-08 危害等级:    漏洞类 ...

  7. 搜索(DLX):HDU 3663 Power Stations

    Power Stations Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. 【模拟】ECNA 2015 I What's on the Grille? (Codeforces GYM 100825)

    题目链接: http://codeforces.com/gym/100825 题目大意: 栅栏密码.给定N(N<=10),密钥为一个N*N的矩阵,'.'代表空格可以看到,'X'代表被遮挡,还有密 ...

  9. QTP关于AOM的Javascript启动方式

    序 QTP的AOM模型想必大家都很熟悉了,平时常用的就是通过VBS脚本的方式编写启动程序(也是我现在用的方法).其实,还有很多其他的方式,如Java,C#,JS,这些语言都是通过调用QTObjectM ...

  10. 如何解决升级WIN服务的时候,不暴力停止服务 达到升级的目的

    同一个WIN服务,分别部署在A.B两台服务器上,前面使用netscaler负载均衡 ,A和B被请求频繁,几乎时时刻刻都被请求   .PS:发布WIN服务的正常流程是,停止WIN服务->发布WIN ...