EF 到值类型“System.Decimal”的强制转换失败,因为具体化值为 null。(转载)

解决方案:

 DateTime?   date = dbfw.TMS10min.Select(d => (DateTime?)d.TIMESTAMP).Max();

 

 decimal? sum = _member.DbSet
.Where(q => q.MemberID == 11)
.Sum(q => (decimal?)q.ActiveAmount); //核心

 

转载来源:https://blog.csdn.net/u011127019/article/details/53909325

一、使用EF访问数据库求和时经常遇到这样的异常

“System.InvalidOperationException”类型的未经处理的异常在 EntityFramework.dll 中发生 

其他信息: 到值类型“System.Decimal”的强制转换失败,因为具体化值为 null。结果类型的泛型参数或查询必须使用可以为 null 的类型。

截图:

异常代码1:

 

  1.  
    decimal sum = _member.DbSet
  2.  
    .Where(q => q.MemberID == 11)
  3.  
    .Sum(q => q.ActiveAmount);

异常代码2:

 

  1.  
    decimal sum = _member.DbSet.Where(q => q.MemberID == 11)
  2.  
    .Select(q => q.ActiveAmount)
  3.  
    .Sum();


 

二、原因:

 

抛出的原因:根据条件从数据库筛选出的数据为空,然后求和就抛出异常了。

解决方案:根据条件将筛选字段或数据行取到内存后,在求和

 

  1.  
    //将数据库数据拿到内存再求和
  2.  
    decimal sum = _member.DbSet.Where(q => q.MemberID == 11)
  3.  
    .Select(q => q.ActiveAmount)
  4.  
    .ToList()
  5.  
    .Sum();

解决方案2:如果根据条件当筛选数据行不存在时,求和结果需要指定为null时,可以这样处理

 

  1.  
    decimal? sum = _member.DbSet
  2.  
    .Where(q => q.MemberID == 11)
  3.  
    .Sum(q => (decimal?)q.ActiveAmount); //核心

 

解决方案3(推荐):使用DefaultIfEmpty()

http://blog.csdn.net/u011127019/article/details/61413670

 

一、字符串类型最大值

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

 

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

 

二、数字类型最大值

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

 

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

 

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

 

  1.  
    decimal deci2 = _context.scores.Where(q => false).Max(q => q.degree);
  2.  
    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);

生成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);

解决方案3:

 

 

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

 

更多:

EntiryFramework中事务操作实例

EntityFramework中JSON序列化循环引用----JavaScriptSerializer

 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章