一、字符串类型最大值

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. javascript之反柯里化(uncurrying)

    在JavaScript中,当我们调用对象的某个方法时,其实不用去关心该对象原本是否被设计为拥有这个方法,这是动态类型语言的特点.可以通过反柯里化(uncurrying)函数实现,让一个对象去借用一个原 ...

  2. Git标签(版本)管理

    列出当前所有的标签 git tag     可以搜索特定的标签,例如你只想看稳定版相关的 git tag -l "*.stable"   给当前commit打标签(设定版本) gi ...

  3. URAL 1040 Airline Company 构造,思路 难度:2

    http://acm.timus.ru/problem.aspx?space=1&num=1040 题目要求在一个联通无向图中找出一种方法给边标号使得任意一个有多条边的点,边的号码的最大公约数 ...

  4. 为什么不建议将 font-size 设置为 12px 以下?如果一定要设置为 12px 以下要怎么做?

    问题:为什么不建议将 font-size 设置为 12px 以下?如果一定要设置为 12px 以下要怎么做? 先看看把 font-size 设置为 12px 以下时的效果:(浏览器为 Chrome 5 ...

  5. 更新.xsd后,rdlc 数据源更新不了

  6. SharePoint 2013的100个新功能之开发

    一:SharePoint应用 SharePoint 2013引入了云应用模型来供用户创建应用.SharePoint应用是独立的功能整合,扩展了SharePoint网站的功能.应用可以包含SharePo ...

  7. HDU 2485

    http://acm.hdu.edu.cn/showproblem.php?pid=2485 n个车站,m条边,两边之间费用为1,问最少摧毁多少车站,使得1-n无法在k时间内到达 将2-(n-1)每个 ...

  8. Codeforces 868F. Yet Another Minimization Problem【决策单调性优化DP】【分治】【莫队】

    LINK 题目大意 给你一个序列分成k段 每一段的代价是满足\((a_i=a_j)\)的无序数对\((i,j)\)的个数 求最小的代价 思路 首先有一个暴力dp的思路是\(dp_{i,k}=min(d ...

  9. spring-security-4 (4)spring security 认证和授权原理

    在上一节我们讨论了spring security过滤器的创建和注册原理.请记住springSecurityFilterChain(类型为FilterChainProxy)是实际起作用的过滤器链,Del ...

  10. Windows与.NET Framework、数据库版本对应关系

    ================== windows XP/server 2003 (.NET Framework与 SQL Server\Oracle) 支持.NET版本:.NET Framewor ...