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. STM32启动文件的选择

    移植了同事一个程序,然后死活不能用,发现启动文件错了,明天继续调.真把人折腾死了. stm32给的库文件太琐碎了,正如它的芯片型号一样繁多,例如启动文件: 网上查到的各个文件的解释是: startup ...

  2. 全国省市区Json文件 ,做省市区联动很轻松

    省份 [{"name":"安徽省", "code":"340000"},{"name":" ...

  3. 【Java】String,StringBuffer与StringBuilder的区别??

    String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要性能 ...

  4. 以不同用户身份运行程序,/savecred只需要输入一次密码(GetTokenByName取得EXPLORER.EXE的令牌,然后调用CreateProcessAsUser,而且使用LoadUserProfile解决另存文件的问题)good

    http://blog.sina.com.cn/s/blog_65977dde0100s7tm.html ----------------------------------------------- ...

  5. Ehcache中配置详解

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc ...

  6. java实战之数组工具集

    java是一门面向对象的语言,我们也提到过,面向对象的一个优点就在于能够提高代码的复用性,前面我们详细讲过数组的查找,排序,等等,为了提高代码的复用性,我们何不自己写一个数组的工具集,来综合下前面所学 ...

  7. Qt入门(16)——组装窗口部件

    这个例子显示了创建几个窗口部件并用信号和槽把它们连接起来,和如何处理重新定义大小事件. #include <qapplication.h> #include <qpushbutton ...

  8. CodeForces 591A

    题目链接: http://codeforces.com/problemset/problem/591/A 题意: a,b两个相距L距离,两个分别以p,q速度从左右两个端点出发,每次相遇后,又以原来的速 ...

  9. wamp中的httpd.conf文件设置

    ServerName localhost 缺省情况下,并不需要指定这个ServerName参数,服务器将自动通过名字解析过程来获得自己的名字,但如果服务器的名字解析有问题(通常为反向解析不正确),或者 ...

  10. 外星人的供给站 (区间覆盖 t贪心)

    /** 区间覆盖问题 分析: 每个点可以确定两个圆心 圆心的范围形成 一个区间 在这个区间上以任意一点画圆便可将此点 包含在内 如果有两个点所确定的区间相交了 说明这两个点可以用一个圆包含在内 即用一 ...