Enumerable.Single和Enumerable.SingleOrDefault方法

简介:

Enumerable.Single方法,返回集合中唯一的一条记录,如果记录数不等于1就报错。

Enumerable.SingleOrDefault方法,返回集合中唯一的一条记录,如果记录数大于1报错,如果记录数等于0,则返回默认值。对于引用类型和可空类型的数据,这个默认值就是null。
有时候返回的这个null不是很方便,可以通过扩展方法,扩展一个SingleOrNew方法。
具体代码如下:
 

 
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.          
  6.         List<test> l = new List<test>(){  
  7.         new test(),  
  8.         new test()  
  9.         };  
  10.           
  11.         var o1 = l.SingleOrNew<test>(m => m.a == 0);//匹配2个,异常  
  12.         var o2 = l.SingleOrNew<test>(m => m.a == 70);//匹配0个,返回new test()  
  13.  
  14.     }  
  15. }  
  16.  
  17. class test  
  18. {  
  19.     public int a;  
  20.     public string s;  
  21. }  
  22.  
  23. static class Extension  
  24. {  
  25.     public static T SingleOrNew<T>(this IEnumerable<T> query) where T : new()  
  26.     {  
  27.         if (query.Count() == 0)  
  28.         {  
  29.             return new T();  
  30.         }  
  31.         return query.Single();  
  32.     }  
  33.  
  34.     public static T SingleOrNew<T>(this IEnumerable<T> query, Func<T, bool> predicate) where T : new()  
  35.     {  
  36.         if (query.Count(predicate) == 0)  
  37.         {  
  38.             return new T();  
  39.         }  
  40.         return query.Single(predicate);  
  41.     }  
  42. }  

 














本文转自cnn23711151CTO博客,原文链接: http://blog.51cto.com/cnn237111/1001731,如需转载请自行联系原作者



相关文章
|
24天前
|
JavaScript
Component name “header“ should always be multi-word
Component name “header“ should always be multi-word
|
2月前
shouldComponentUpdate有什么作用
shouldComponentUpdate有什么作用
32 0
|
11月前
|
JSON 数据格式
成功解决 global init errTypeError:Right-hand side of 'instanceof' is not callable
成功解决 global init errTypeError:Right-hand side of 'instanceof' is not callable
|
11月前
913 error Component name “home“ should always be multi-word vuemulti-word-component-names
913 error Component name “home“ should always be multi-word vuemulti-word-component-names
70 0
|
数据库
Field ‘id‘ doesn‘t have a default value
Field ‘id‘ doesn‘t have a default value
137 0
Multiple substitutions specified in non-positional format; did you mean to add BUG(7)
Multiple substitutions specified in non-positional format; did you mean to add BUG(7)
Optional int parameter ‘id‘ is present but cannot be translated into a null value due to being ……
Optional int parameter ‘id‘ is present but cannot be translated into a null value due to being ……
236 0
|
JavaScript 前端开发 开发者
Component name “xxx“ should always be multi-word
Component name “xxx“ should always be multi-word
Component name “xxx“ should always be multi-word
|
Java 数据库连接 mybatis
A query was run and no Result Maps were found for the Mapped Statement
A query was run and no Result Maps were found for the Mapped Statement
188 0
How to create unit test for product determination function module
How to create unit test for product determination function module
129 0
How to create unit test for product determination function module