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. React生命周期

    在react生命周期中,分2段执行,一个挂载的生命周期,一个是组件发生了数据变动,或者事件触发而引发的更新生命周期. 注:react生命周期很重要,对于很多组件场景的应用发挥重要作用,而且不熟悉生命周 ...

  2. 背压(Backpressure)机制

    作者:张铁蕾链接:https://www.zhihu.com/question/49618581/answer/117107570来源:知乎著作权归作者所有,转载请联系作者获得授权. 首先,从大的方面 ...

  3. mac 关于使用protobuf出现ld: symbol(s) not found for architecture x86_64的问题

    主要是编译时没有添加protobuf库文件 g++  -o Writer.o  lm.helloworld.pb.cc Writer.cpp -L/usr/local/lib -lprotobuf

  4. ACM :漫漫上学路 -DP -水题

    CSU 1772 漫漫上学路 Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit ...

  5. PLSQL配置新的oracle实例

    在Win环境下的Oracle中,D:\Oracle\product\10.1.0\db_1\NETWORK\ADMIN\tnsnames.ora文件很重要,它作用是:本地命名的配置.本地名可以用简单的 ...

  6. [Protobuf] Mac系统下安装配置及简单使用

    Mac下Protobuf安装 Protobuf源码Github地址: https://github.com/google/protobuf 配置环境教程: https://github.com/goo ...

  7. [Android]依赖注入框架squareup的dagger

    分享一下Android依赖注入框架--Dagger使用 Dagger源码 Dagger1-Demo 希望能给大家的开发带来帮助.

  8. java记录

    1. 包装类与自动装箱问题:在justjavac的博客上看到翻译的一篇文章 离开java,寻找更佳语言的十大理由 中关于自动装箱的一个描述: 这个特性是为了解决因原生类型的存在所导致的问题,在Java ...

  9. Linux软件安装

    #配置/etc/apt/sources.list 通过root权限修改/etc/apt/sources.list $ su #输入密码进入root权限 $ chmod 0666 /etc/apt/so ...

  10. <十六>JDBC_使用 DBUtils 编写通用的DAO

    接口 : DAO<T>.java import java.sql.Connection;import java.sql.SQLException;import java.util.List ...