一、字符串类型最大值

1.字符串类型的最大值,和数据库的字典排序最后一个相同,如果存在返回null

  1. //字符串最大值,是字典排序最后一个
  2. string max1 = _context.students.Max(q => q.sname);
  3. Console.WriteLine(max1);
  4. //字符串最大值,如果不存在返回null
  5. string max2 = _context.students
  6. .Where(q => false)
  7. .Max(q => q.sname);
  8. Console.WriteLine(max2);
  9. Console.WriteLine(max2 == null); //True
  10. Console.WriteLine(max2 == ""); //False
//字符串最大值,是字典排序最后一个
string max1 = _context.students.Max(q => q.sname);
Console.WriteLine(max1); //字符串最大值,如果不存在返回null
string max2 = _context.students
.Where(q => false)
.Max(q => q.sname);
Console.WriteLine(max2);
Console.WriteLine(max2 == null); //True
Console.WriteLine(max2 == ""); //False

二、数字类型最大值

1.数字类型最大值,和数据库字段排序最后一个相同,如果没有数据抛出异常。

  1. //数字类型,获取最大值为正序排列最后一个值
  2. decimal deci1 = _context.scores.Max(q => q.degree);
  3. Console.WriteLine(deci1);
//数字类型,获取最大值为正序排列最后一个值
decimal deci1 = _context.scores.Max(q => q.degree);
Console.WriteLine(deci1);

数字类型,获取条件的数据不存在抛出异常
其他信息: 到值类型“System.Decimal”的强制转换失败,因为具体化值为 null。
结果类型的泛型参数或查询必须使用可以为 null 的类型。

  1. decimal deci2 = _context.scores.Where(q => false).Max(q => q.degree);
  2. Console.WriteLine(deci2);
decimal deci2 = _context.scores.Where(q => false).Max(q => q.degree);
Console.WriteLine(deci2);

解决方案1:

  1. //解决方案1,使用DefaultIfEmpty(),推荐
  2. var query = _context.scores.Where(q => false)
  3. .Select(q => q.degree)
  4. .DefaultIfEmpty();
  5. Console.WriteLine(query.ToString());
  6. decimal deci3 = query
  7. .Max();
  8. Console.WriteLine(deci3);
//解决方案1,使用DefaultIfEmpty(),推荐
var query = _context.scores.Where(q => false)
.Select(q => q.degree)
.DefaultIfEmpty();
Console.WriteLine(query.ToString());
decimal deci3 = query
.Max();
Console.WriteLine(deci3);

生成sql如下:

解决方案2:

  1. //解决方案2,先判断再取值,执行两次数据库查询
  2. decimal deci4 = 0;
  3. if (_context.scores.Any())
  4. {
  5. deci4 = _context.scores.Max(q => q.degree);
  6. }
  7. Console.WriteLine(deci4);
//解决方案2,先判断再取值,执行两次数据库查询
decimal deci4 = 0;
if (_context.scores.Any())
{
deci4 = _context.scores.Max(q => q.degree);
}
Console.WriteLine(deci4);

解决方案3:

  1. //解决方案3,内存取最大值
  2. decimal deci5 = _context.scores
  3. .Select(q => q.degree)
  4. .ToList()
  5. .Max();
  6. Console.WriteLine(deci5);
//解决方案3,内存取最大值
decimal deci5 = _context.scores
.Select(q => q.degree)
.ToList()
.Max();
Console.WriteLine(deci5);

EF 汇总函数使用注意事项Max()/Min()等的更多相关文章

  1. 6.组函数(avg(),sum(),max(),min(),count())、多行函数,分组数据(group by,求各部门的平均工资),分组过滤(having和where),sql优化

     1组函数 avg(),sum(),max(),min(),count()案例: selectavg(sal),sum(sal),max(sal),min(sal),count(sal) from ...

  2. 74.Python中ORM聚合函数详解:Max,Min

    Max和Min:获取指定对象的最大值和最小值. 1. 比如:想要获取Author表中的最大的年龄和最小的年龄.示例代码如下: from django.http import HttpResponse ...

  3. MySQL之汇总数据(AVG,COUNT,MAX,MIN,SUM)

    table test Field Type Null Key Default Extra id int(11) NO PRI NULL auto_increment name char(50) NO ...

  4. 聚合函数:sum,avg,max,min,count

    group by  分组的使用方法 数学函数:ABS.ceiling.floor.power.round.sqrt.square 练习:

  5. max,min,Zip函数(十一)

    zip函数,拉链,传两个有序的参数,将他们一一对应为元祖形式 max,min比较默认比较一个元素,处理的是可迭代对象,相当于for循环取出每个元素进行比较,注意:不同类型之间不可比较 #!/usr/b ...

  6. group by与avg(),max(),min(),sum()函数的关系

    数据库表: create table pay_report(     rdate varchar(8),     --日期     region_id varchar(4),    --地市      ...

  7. MDX Step by Step 读书笔记(七) - Performing Aggregation 聚合函数之 Max, Min, Count , DistinctCount 以及其它 TopCount, Generate

    MDX 中最大值和最小值 MDX 中最大值和最小值函数的语法和之前看到的 Sum 以及 Aggregate 等聚合函数基本上是一样的: Max( {Set} [, Expression]) Min( ...

  8. Python之路Python内置函数、zip()、max()、min()

    Python之路Python内置函数.zip().max().min() 一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算, ...

  9. SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum

    SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum avg() 函数 定义和用法 AVG 函数返回数值列的平均值.NULL ...

随机推荐

  1. 雷林鹏分享:Ruby CGI Sessions

    Ruby CGI Sessions CGI::Session 可以为用户和CGI环境保存持久的会话状态,会话使用后需要关闭,这样可以保证数据写入到存储当中,当会话完成后,你需要删除该数据. #!/us ...

  2. Xmind8 (update8)破解教程

    环境:Win7 一.下载xmind: Xmind版本:xmind-8-update8-windows.exe  百度云盘地址: https://pan.baidu.com/s/1ccd18E1hOY1 ...

  3. 【MySQL】经典数据库SQL语句编写练习题——SQL语句扫盲

    [MySQL]数据库原理复习——SQL语言 对基本的SQL语句编写的练习题,其中的题目的答案可能会有多种书写方式. 1.题目1 1.1 关系模式 学生student:SNO:学号,SNAME:姓名,A ...

  4. vijos 1082

    描述 东非大裂谷中有一片神秘的丛林,是全世界探险家的乐园,著名黄皮肤探险家BB一直想去试试.正好我国科学家2005年4月将首次对东非大裂谷进行科考,BB决定随科考队去神秘丛林探险.在出发之前,他搜集了 ...

  5. C#中as和is关键字

    一. as 运算符用于在兼容的引用类型之间执行某些类型的转换.例如: static void Main(string[] args) { ]; obj[] = new class1(); obj[] ...

  6. html绘制三角形(兼容IE6)

    .sanjiao { width:; height:; overflow: hidden; border-width: 10px; border-color: red transparent tran ...

  7. Microsoft Office 2010 Service Pack 2 发布更新

    Microsoft Office 2010 32 位版本 的 Service Pack 2 (SP2) 包含的新更新可以提高安全性.性能和稳定性.此外,SP 是以前发布的所有更新的汇总. 下载更新补丁 ...

  8. Git Error: warning: refname 'origin/branch-name' is ambiguous.

    When this happened, it created the file .git/refs/heads/origin/branch-name. So, I just deleted the f ...

  9. Linux DMA Engine framework(2)_功能介绍及解接口分析

    http://www.wowotech.net/linux_kenrel/dma_engine_api.html 补充 http://www.zhimengzhe.com/linux/259646.h ...

  10. VMware上安装CenterOS

    1.环境:Win10.VMware Workstation 12.Centeros 7 2.VMware workstation12安装 双击“VMware_workstation_full_12.5 ...