1. CASE WHEN ... THEN ...

var expr = from p in context.Products
select new
{
商品ID = p.ProductID,
商品名称 = p.ProductName,
是否库存 = p.UnitsInStock > ? "是" : "否"
};
SELECT
[Extent1].[ProductID] AS [ProductID],
[Extent1].[ProductName] AS [ProductName],
CASE WHEN ([Extent1].[UnitsInStock] > 0) THEN N'是' ELSE N'否' END AS [C1]
FROM [dbo].[Product] AS [Extent1]

2. Distinct

var expr = context.Products
.Select(p => p.CategoryID)
.Distinct();
SELECT
[Distinct1].[CategoryID] AS [CategoryID]
FROM ( SELECT DISTINCT
[Extent1].[CategoryID] AS [CategoryID]
FROM [dbo].[Product] AS [Extent1]
) AS [Distinct1]

3. Count

var expr = context.Products.Count();
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[Product] AS [Extent1]
) AS [GroupBy1]
var expr = context.Products
.Count(p => p.UnitPrice > 10m);
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[Product] AS [Extent1]
WHERE [Extent1].[UnitPrice] > cast(10 as decimal(18))
) AS [GroupBy1]

4. LongCount

var expr = context.Products.LongCount();
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT_BIG(1) AS [A1]
FROM [dbo].[Product] AS [Extent1]
) AS [GroupBy1]
var expr = context.Products
.LongCount(p => p.UnitPrice > 10m);
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT_BIG(1) AS [A1]
FROM [dbo].[Product] AS [Extent1]
WHERE [Extent1].[UnitPrice] > cast(10 as decimal(18))
) AS [GroupBy1]

5. Sum

var expr = context.Products
.Select(p=>p.UnitsInStock)
.Sum();
var expr = context.Products
.Sum(p => p.UnitsInStock);
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
SUM([Extent1].[UnitsInStock]) AS [A1]
FROM [dbo].[Product] AS [Extent1]
) AS [GroupBy1]

6. Min

var expr = context.Products
.Min(p => p.UnitPrice);
var expr = context.Products
.Select(p => p.UnitPrice)
.Min();
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
MIN([Extent1].[UnitPrice]) AS [A1]
FROM [dbo].[Product] AS [Extent1]
) AS [GroupBy1]

  查找每个类别中单价最低的商品:

var expr = from p in context.Products
group p by p.CategoryID into g
select new
{
CategoryID = g.Key,
CheapestProducts = from p2 in g
where p2.UnitPrice == g.Min(p3 => p3.UnitPrice)
select p2
};
SELECT
[Project1].[CategoryID] AS [CategoryID],
[Project1].[C1] AS [C1],
[Project1].[ProductID] AS [ProductID],
[Project1].[CategoryID1] AS [CategoryID1],
[Project1].[ProductName] AS [ProductName],
[Project1].[UnitPrice] AS [UnitPrice],
[Project1].[UnitsInStock] AS [UnitsInStock],
[Project1].[Discontinued] AS [Discontinued]
FROM ( SELECT
[GroupBy1].[K1] AS [CategoryID],
[Extent2].[ProductID] AS [ProductID],
[Extent2].[CategoryID] AS [CategoryID1],
[Extent2].[ProductName] AS [ProductName],
[Extent2].[UnitPrice] AS [UnitPrice],
[Extent2].[UnitsInStock] AS [UnitsInStock],
[Extent2].[Discontinued] AS [Discontinued],
CASE WHEN ([Extent2].[ProductID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM (SELECT
[Extent1].[CategoryID] AS [K1],
MIN([Extent1].[UnitPrice]) AS [A1]
FROM [dbo].[Product] AS [Extent1]
GROUP BY [Extent1].[CategoryID] ) AS [GroupBy1]
LEFT OUTER JOIN [dbo].[Product] AS [Extent2] ON ([GroupBy1].[K1] = [Extent2].[CategoryID]) AND ([Extent2].[UnitPrice] = [GroupBy1].[A1])
) AS [Project1]
ORDER BY [Project1].[CategoryID] ASC, [Project1].[C1] ASC

7. Max

var expr = context.Products
.Max(p => p.UnitPrice);
var expr = context.Products
.Select(p => p.UnitPrice)
.Max();
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
MAX([Extent1].[UnitPrice]) AS [A1]
FROM [dbo].[Product] AS [Extent1]
) AS [GroupBy1]

8.Avg

var expr = context.Products
.Select(p => p.UnitPrice)
.Average();
var expr = context.Products
.Average(p => p.UnitPrice);
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
AVG([Extent1].[UnitPrice]) AS [A1]
FROM [dbo].[Product] AS [Extent1]
) AS [GroupBy1]

  查找单个类别中单价高于平均价的产品:

var expr = from p in context.Products
group p by p.CategoryID into g
select new
{
CategoryID = g.Key,
ExpensiveProducts = from p2 in g
where p2.UnitPrice > g.Average(p3 => p3.UnitPrice)
select p2
};
SELECT
[Project1].[CategoryID] AS [CategoryID],
[Project1].[C1] AS [C1],
[Project1].[ProductID] AS [ProductID],
[Project1].[CategoryID1] AS [CategoryID1],
[Project1].[ProductName] AS [ProductName],
[Project1].[UnitPrice] AS [UnitPrice],
[Project1].[UnitsInStock] AS [UnitsInStock],
[Project1].[Discontinued] AS [Discontinued]
FROM ( SELECT
[GroupBy1].[K1] AS [CategoryID],
[Extent2].[ProductID] AS [ProductID],
[Extent2].[CategoryID] AS [CategoryID1],
[Extent2].[ProductName] AS [ProductName],
[Extent2].[UnitPrice] AS [UnitPrice],
[Extent2].[UnitsInStock] AS [UnitsInStock],
[Extent2].[Discontinued] AS [Discontinued],
CASE WHEN ([Extent2].[ProductID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM (SELECT
[Extent1].[CategoryID] AS [K1],
AVG([Extent1].[UnitPrice]) AS [A1]
FROM [dbo].[Product] AS [Extent1]
GROUP BY [Extent1].[CategoryID] ) AS [GroupBy1]
LEFT OUTER JOIN [dbo].[Product] AS [Extent2] ON ([GroupBy1].[K1] = [Extent2].[CategoryID]) AND ([Extent2].[UnitPrice] > [GroupBy1].[A1])
) AS [Project1]
ORDER BY [Project1].[CategoryID] ASC, [Project1].[C1] ASC

LINQ系列:LINQ to SQL Transact-SQL函数的更多相关文章

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

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

  2. LINQ to SQL 调用 SQL Server 的系统函数

    Ø  简介 在 C# 中比较常用的 ORM(Object Relational Mapping)框架就是 EF 了,EF 经常结合 LINQ to SQL 来操作数据库.本文主要讨论如何在 LINQ ...

  3. Linq之Linq to Sql

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

  4. SQL Server游标 C# DataTable.Select() 筛选数据 什么是SQL游标? SQL Server数据类型转换方法 LinQ是什么? SQL Server 分页方法汇总

    SQL Server游标   转载自:http://www.cnblogs.com/knowledgesea/p/3699851.html. 什么是游标 结果集,结果集就是select查询之后返回的所 ...

  5. Spark 系列(十一)—— Spark SQL 聚合函数 Aggregations

    一.简单聚合 1.1 数据准备 // 需要导入 spark sql 内置的函数包 import org.apache.spark.sql.functions._ val spark = SparkSe ...

  6. LinQ系列文章

    温故而知新,想着系统再学习一次LinQ知识点,发现园子里有个非常棒的系列文章,所以Mark下来,方便以后查阅! 系列博客导航: LINQ之路系列博客导航 LINQ之路 1:LINQ介绍 LINQ之路 ...

  7. LINQ 系列

    C#图解教程 第十九章 LINQ   LINQ 什么是LINQLINQ提供程序 匿名类型 方法语法和查询语法查询变量查询表达式的结构 from子句join子句什么是联结查询主体中的from…let…w ...

  8. SQL Server自定义函数( 转载于51CTO )

    用户自定义函数自定义函数不能执行一系列改变数据库状态的操作,可以像系统函数在查询或存储过程等的程序中使用,也可以像相信过程一样能过 execute 命令来执行.自定义函数中存储了一个 Transact ...

  9. 【翻译】Flink Table Api & SQL — 内置函数

    本文翻译自官网:Built-In Functions  https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/fu ...

  10. SQL Server 聚合函数算法优化技巧

    Sql server聚合函数在实际工作中应对各种需求使用的还是很广泛的,对于聚合函数的优化自然也就成为了一个重点,一个程序优化的好不好直接决定了这个程序的声明周期.Sql server聚合函数对一组值 ...

随机推荐

  1. 简易版C语言程序语法

    <程序> -〉 <外部声明> | <函数定义><外部声明> -〉<头文件> | <变量> | <结构体> <头 ...

  2. 【转】iOS开发 -- Apple Pay

    技术博客原地址:http://www.cnblogs.com/dashunzi/p/ApplePay.html#top 原技术博客中有源码和视频,有感兴趣的朋友可以研究一下! 一.什么是Apple P ...

  3. js基础2

    什么是DOM? DOM:文档对象模型(Document Object Model),又称为文档树模型.是一套操作HTML文档的API. 什么是文档对象模型? DOM将html文档看成了一个对象,htm ...

  4. angular 中父元素ng-repeat后子元素ng-click失效

    在angular中使用ng-repeat后ng-click失效,今天在这个上面踩坑了.特此记录一下. 因为ng-repeat创造了新的SCOPE.如果要使用这个scope的话就必须使用$parent来 ...

  5. lucene和es总结

    一.首先介绍lucene涉及到的排序过程 1.1.如何自定义排序对象 你可以自定义collector对象: 亦可以自定义comparator对象: 可以自定义scoredoc对象,决定如何处理结果集合 ...

  6. 把token带到 http头部 或者验证一下referer

    提交地址:http://baozoumanhua.com/users/8311358提交数据:-----------------------------195704664324Content-Disp ...

  7. opencv算法学习

    1.改变图像的亮度和对比度: 算法介绍:对每一点像素值的r,g,b,值进行乘法和加法的运算. 代码使用: ; y < image.rows; y++ ) { ; x < image.col ...

  8. ant 自动化编译

    bulid.xml配置: <?xml version="1.0" encoding="UTF-8" ?> <project name=&quo ...

  9. Hadoop 和 HDInsight:Windows Azure 中的大数据

    世界的大数据包含一个庞大而充满活力的生态系统,但一个开放源码项目上面有这一切,那就是 Hadoop 的王朝. Hadoop 是事实上的标准的分布式的数据运算.Hadoop 提供了一个 MapReduc ...

  10. .net core中使用openssl的公钥私钥进行加解密

    这篇博文分享的是 C#中使用OpenSSL的公钥加密/私钥解密 一文中的解决方法在 .net core 中的改进.之前的博文针对的是 .NET Framework ,加解密用的是 RSACryptoS ...