1. 一对多

var expr = context.Products
.Where(p => p.Category.CategoryName == "LINQ to SQL" && p.UnitPrice > 10m)
.Select(p => new
{
p.ProductID,
p.ProductName
});
var expr = from p in context.Products
where p.Category.CategoryName == "LINQ to SQL" && p.UnitPrice > 10m
select new
{
p.ProductID,
p.ProductName
};
SELECT
[Extent1].[ProductID] AS [ProductID],
[Extent1].[ProductName] AS [ProductName]
FROM [dbo].[Product] AS [Extent1]
INNER JOIN [dbo].[Category] AS [Extent2] ON [Extent1].[CategoryID] = [Extent2].[CategoryID]
WHERE (N'LINQ to SQL' = [Extent2].[CategoryName]) AND ([Extent1].[UnitPrice] > cast(10 as decimal(18)))
var expr = from p in context.Products
where p.UnitPrice > 10m
join c in context.Categories on p.CategoryID equals c.CategoryID
select new
{
p.ProductID,
p.ProductName,
c.CategoryName
};
SELECT
[Extent1].[ProductID] AS [ProductID],
[Extent1].[ProductName] AS [ProductName],
[Extent2].[CategoryName] AS [CategoryName]
FROM [dbo].[Product] AS [Extent1]
INNER JOIN [dbo].[Category] AS [Extent2] ON [Extent1].[CategoryID] = [Extent2].[CategoryID]
WHERE [Extent1].[UnitPrice] > cast(10 as decimal(18))
var expr = from p in context.Products
join c in context.Categories on p.CategoryID equals c.CategoryID
select new
{
p.ProductID,
p.ProductName,
c.CategoryName
};
SELECT
[Extent1].[ProductID] AS [ProductID],
[Extent1].[ProductName] AS [ProductName],
[Extent2].[CategoryName] AS [CategoryName]
FROM [dbo].[Product] AS [Extent1]
INNER JOIN [dbo].[Category] AS [Extent2] ON [Extent1].[CategoryID] = [Extent2].[CategoryID]

2. 双向关联

var expr = from c in context.Categories
join p in context.Products on c.CategoryID equals p.CategoryID into cp
select new
{
c.CategoryName,
TotalCategoryProducts = cp.Count()
};
SELECT
[Extent1].[CategoryID] AS [CategoryID],
[Extent1].[CategoryName] AS [CategoryName],
(SELECT
COUNT(1) AS [A1]
FROM [dbo].[Product] AS [Extent2]
WHERE [Extent1].[CategoryID] = [Extent2].[CategoryID]) AS [C1]
FROM [dbo].[Category] AS [Extent1]

3. left out join

var expr = from c in context.Categories
join p in context.Products on c.CategoryID equals p.CategoryID into CategoryProducts
from cp in CategoryProducts.DefaultIfEmpty()
select new
{
c.CategoryID,
c.CategoryName,
TotalProducts = CategoryProducts.Count()
};
SELECT
[Extent1].[CategoryID] AS [CategoryID],
[Extent1].[CategoryName] AS [CategoryName],
(SELECT
COUNT(1) AS [A1]
FROM [dbo].[Product] AS [Extent3]
WHERE [Extent1].[CategoryID] = [Extent3].[CategoryID]) AS [C1]
FROM [dbo].[Category] AS [Extent1]
LEFT OUTER JOIN [dbo].[Product] AS [Extent2] ON [Extent1].[CategoryID] = [Extent2].[CategoryID]

4. inner join select many

var expr = from c in context.Categories
from p in c.Products
where c.CategoryName == "LINQ to SQL"
select p;
var expr = context.Categories
.Where(c => c.CategoryName == "LINQ to SQL")
.SelectMany(c => c.Products);
SELECT
[Extent2].[ProductID] AS [ProductID],
[Extent2].[CategoryID] AS [CategoryID],
[Extent2].[ProductName] AS [ProductName],
[Extent2].[UnitPrice] AS [UnitPrice],
[Extent2].[UnitsInStock] AS [UnitsInStock],
[Extent2].[Discontinued] AS [Discontinued]
FROM [dbo].[Category] AS [Extent1]
INNER JOIN [dbo].[Product] AS [Extent2] ON [Extent1].[CategoryID] = [Extent2].[CategoryID]
WHERE N'LINQ to SQL' = [Extent1].[CategoryName]

LINQ系列:LINQ to SQL Join连接的更多相关文章

  1. 【SQL】各取所需 | SQL JOIN连接查询各种用法总结

    前面 在实际应用中,大多的查询都是需要多表连接查询的,但很多初学SQL的小伙伴总对各种JOIN有些迷糊.回想一下,初期很长一段时间,我常用的似乎也就是等值连接 WHERE 后面加等号,对各种JOIN也 ...

  2. SQL Join连接

    SQL 连接(Joins) SQL join 用于把来自两个或多个表的行结合起来. SQL JOIN SQL JOIN 子句用于把来自两个或多个表的行结合起来,基于这些表之间的共同字段. 最常见的 J ...

  3. SQL join 连接时 条件加在 on后面和 where 的区别

    task 是用户任务表,manageuser是用户表,以left join 为参考: 此时主表是task,三条sql语句:注意区别.第一句无筛选条件,第二句筛选条件在on后面,第三句sql的筛选语句放 ...

  4. SQL JOIN连接分类[转]

    1.内联接(典型的联接运算,使用像 =  或 <> 之类的比较运算符):包括相等联接和自然联接: 内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行:    2.外联接.外联接可以 ...

  5. SQL Join(连接查询)

    1.连接查询分为: inner join(自然连接,自连接) Left join(左连接)/Left outer join(左外连接):效果一样 Right join(右连接)/Right outer ...

  6. SQL Join连接大小表在前在后的重要性(小表在前提高执行效率)

    引用地址:https://blog.csdn.net/qq_30349961/article/details/82662550 http://blog.sina.com.cn/s/blog_6ff05 ...

  7. LINQ系列目录

    1. LINQ准备 1.1 C#中与LINQ相关特性 2. LINQ to Object 2.1 LINQ to Object投影操作符(Select/SelectMany/Let) 2.2 LINQ ...

  8. C# ~ 从 XML 到 Linq 到 Linq to XML

    .XML 可扩展标记语言 (Extensible Markup Language), 标记 (markup) 是关键部分,是标准通用标记语言 (Standard Generalized Markup ...

  9. Linq之Linq to Sql

    目录 写在前面 系列文章 Linq to sql 总结 写在前面 上篇文章介绍了linq to xml的相关内容,linq to xml提供一种更便捷的创建xml树,及查询的途径.这篇文章将继续介绍l ...

随机推荐

  1. 学习CSS3动画(animation)

    CSS3就是出了不少高大上的功能,3D效果.动画.多列等等.今天写篇文章记录怎么一下怎么用CSS3写一个动画. 丑话还得说前头,IE9以及以下版本不支持CSS3动画(如真要实现可以考虑用js,不过估计 ...

  2. log4j使用--http://www.cnblogs.com/eflylab/archive/2007/01/11/618001.html

    package log4jTest.com; import java.io.FileReader; import org.apache.log4j.BasicConfigurator; import ...

  3. Print a Binary Tree in Vertical Order

    http://www.geeksforgeeks.org/print-binary-tree-vertical-order/ package algorithms; import java.util. ...

  4. jpeg相关知识

    一.jpeg介绍 JPEG 是 Joint Photographic Exports Group 的英文缩写,中文称之为联合图像专家小组.该小组隶属于 ISO 国际标准化组织,主要负责定制静态数字图像 ...

  5. (转)python requests的安装与简单运用

    requests是python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢?官方文档中是这样说明的: python的标准库urllib ...

  6. Grunt 使用记录

    想了解Grunt,可以先去官网 看看. 第一次接触Grunt是通过Coding的移动端项目, 刚开始因为环境的问题折腾了一两天,然后就顿悟了. Grunt构建工具对于前端开发而言,简直是神器(ps.虽 ...

  7. #1094 : Lost in the City

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He does not know where he is ...

  8. wamp2.5 局域网无法访问问题

    1.打开http.conf文件,在对应处修改为如下内容(通常经过步骤一之后就能访问了,若不行则再执行后面步骤) <Directory /> Options FollowSymLinks A ...

  9. 使用JavaScript获取日期加随机数生成单号

    今天学习Javascript,得到一个自动生成单号的JavaScript,留下日后备用: function getNowFormatDate() { var day = new Date(); var ...

  10. Android再学习

    1.点击事件的几种实现方式 Button Btn1 = (Button)findViewById(R.id.button1);//获取按钮资源 Btn1.setOnClickListener(new ...