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,如需转载请自行联系原作者



相关文章
|
5月前
shouldComponentUpdate有什么作用
shouldComponentUpdate有什么作用
20 0
|
9月前
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
50 0
|
12月前
|
搜索推荐 索引
Term Suggester 中 suggest_mode 的三种模式missing、popular、always 的区别
Term Suggester 中 suggest_mode 的三种模式missing、popular、always 的区别
|
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
162 0
《Towards A Fault-Tolerant Speaker Verification System A Regularization Approach To Reduce The Condition Number》电子版地址
Towards A Fault-Tolerant Speaker Verification System: A Regularization Approach To Reduce The Condition Number
64 0
《Towards A Fault-Tolerant Speaker Verification System A Regularization Approach To Reduce The Condition Number》电子版地址
|
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
How to create unit test for product determination function module
How to create unit test for product determination function module
117 0
How to create unit test for product determination function module
A simple tool to calculate the total size of a BSP application
Today Ben asks me whether there is some tool which can allow us to get a draft estimation on the size of a BSP application. As far as I know there is no such tool, so I write one by myself:
99 0
A simple tool to calculate the total size of a BSP application
|
算法
PaperNotes Instance-Level Salient Object Segmentation
title: PaperNotes Instance-Level Salient Object Segmentation comments: true date: 2017-12-20 13:53:11 description: updated: categories: tags: --- https://arxiv.org/pdf/1704.03604.pdf 摘要 现有的显著性检测算法被DL带了一波节奏,但是好像还没有说哪个方法能在显著性区域中找出object instance。
1866 0