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. 使用Android Butterknife

    我之前浏览过android butterknife 的使用 在android studio 中,很惊喜,已经成为一个插件来使用 这个android butterknife 最大的用处,就是直接生成la ...

  2. 【BZOJ3172】[Tjoi2013]单词 AC自动机

    [BZOJ3172][Tjoi2013]单词 Description 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次. Input ...

  3. python基础06 循环

      循环用于重复执行一些程序. for循环  for循环需要预先设定循环的次数n,然后执行隶属于for的语句. 基本构造是 for 元素 in 序列: statement 如: for a in [1 ...

  4. knockoutjs扩展与使用

    原来考虑使用avalon2.0 经过一周的试验,能力不够,用不起来.最终使用了knockout-3.4.js <!DOCTYPE html> <html> <head&g ...

  5. 使用VisualVM分析性能

    性能分析神器VisualVM VisualVM 是一款免费的,集成了多个 JDK 命令行工具的可视化工具,它能为您提供强大的分析能力,对 Java 应用程序做性能分析和调优.这些功能包括生成和分析海量 ...

  6. bootstrap之伪元素

    bootstrap之伪元素 参考地址:http://www.cnblogs.com/keyi/p/5943178.html http://www.runoob.com/css/css-pseudo-e ...

  7. java学习-关于字符串String

    有必要总结记录一下java的学习,否则,永远只是记忆碎片化和always google(费时) 刚好,小伙伴给了一份自己做的review,在学习的过程中,update一下自己的见解和学习内容: 关于S ...

  8. [LintCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  9. iOS app上架需要提前准备的东西

    APP icon,要求1024*1024并且不能有圆角效果. 不同屏幕的截图 3.5的,4的,4.7的,5.5的 测试账号,即登录的账号密码(不能删除或更改的) 联系人电话,电子邮件 对项目的描述 关 ...

  10. nfs配置注意点

    #ip与括号之间不能有空格,不加rw参数时挂载的目录是只读的 /eqp/export/ 10.10.30.0/24(rw,sync,no_root_squash) #更改目录所属的组和所属的用户(nf ...