博客
关于我
LINQ之Single,SingleOrDefault
阅读量:315 次
发布时间:2019-03-04

本文共 2949 字,大约阅读时间需要 9 分钟。

C# 中的 Single() 和 SingleOrDefault() 方法

在 C# 中,Single()SingleOrDefault() 是 LINQ(Language Integrated Query) 方法中非常有用的工具。它们的目的是从一个集合中获取唯一的元素,或者在找不到元素时返回默认值。以下是这些方法的详细解释。


Single()

Single() 方法用于获取集合中的唯一元素。如果集合中有多个元素,它会抛出 InvalidOperationException 异常。如果集合为空,它也会抛出相同的异常。

示例

public static class Program
{
public static void Main(string[] args)
{
int[] numbers = new int[] { 5 };
int result = numbers.Single();
Console.WriteLine("数据:{0}", numbers.Text());
Console.WriteLine("结果:{0}", result);
Console.ReadKey();
}
public static string Text(this IEnumerable
source)
{
string text = string.Empty;
foreach (var value in source)
{
text += string.Format("[{0}], ", value);
}
return text;
}
}

注意事项

  • 多个元素:如果集合中有多个元素,Single() 会抛出 System.InvalidOperationException
  • 空集合:如果集合为空,同样会抛出 System.InvalidOperationException
  • 默认值Single() 没有默认值,总是要求唯一的元素。

SingleOrDefault()

SingleOrDefault()Single() 类似,但在找不到元素时会返回指定的默认值。这个方法在处理空集合或没有匹配元素时非常有用。

示例

public static class Program
{
public static void Main(string[] args)
{
int[] numbers = new int[] { };
int result = 0;
try
{
result = numbers.SingleOrDefault();
}
catch (System.Exception i_exception)
{
Console.WriteLine("异常:{0}", i_exception);
Console.ReadKey();
return;
}
Console.WriteLine("数据:{0}", numbers.Text());
Console.WriteLine("结果:{0}", result);
Console.ReadKey();
}
public static string Text(this IEnumerable
source)
{
string text = string.Empty;
foreach (var value in source)
{
text += string.Format("[{0}], ", value);
}
return text;
}
}

示例(带条件)

public static class Program
{
public static void Main(string[] args)
{
int[] numbers = new int[] { 1, 2, 3, 5, 7, 11 };
int result = 0;
try
{
result = numbers.SingleOrDefault(value => value > 20);
}
catch (System.Exception i_exception)
{
Console.WriteLine("异常:{0}", i_exception);
Console.ReadKey();
return;
}
Console.WriteLine("数据:{0}", numbers.Text());
Console.WriteLine("结果:{0}", result);
Console.ReadKey();
}
public static string Text(this IEnumerable
source)
{
string text = string.Empty;
foreach (var value in source)
{
text += string.Format("[{0}], ", value);
}
return text;
}
}

注意事项

  • 默认值SingleOrDefault() 允许指定一个默认值,用于在找不到元素时返回。
  • 多个匹配元素:如果筛选条件匹配多个元素,SingleOrDefault() 会抛出 System.InvalidOperationException
  • 空集合:如果集合为空,且没有指定默认值,方法会抛出 System.InvalidOperationException

总结

Single()SingleOrDefault() 都是 LINQ 方法库中强大的工具。它们可以帮助我们在集合中快速找到唯一的元素,或者在找不到元素时返回默认值。然而,在使用这些方法时,必须确保集合中只有一个符合条件的元素,否则会抛出异常。

转载地址:http://ffnq.baihongyu.com/

你可能感兴趣的文章
Objective-C实现hardy ramanujana定理算法(附完整源码)
查看>>
Objective-C实现harmonic series调和级数算法(附完整源码)
查看>>
Objective-C实现harris算法(附完整源码)
查看>>
Objective-C实现HashTable哈希表算法(附完整源码)
查看>>
Objective-C实现haversine distance斜距算法(附完整源码)
查看>>
Objective-C实现heap sort堆排序算法(附完整源码)
查看>>
Objective-C实现heap堆算法(附完整源码)
查看>>
Objective-C实现highest response ratio next高响应比优先调度算法(附完整源码)
查看>>
Objective-C实现hill climbing爬山法用来寻找函数的最大值算法(附完整源码)
查看>>
Objective-C实现Hopcroft算法(附完整源码)
查看>>
Objective-C实现hornerMethod霍纳法算法(附完整源码)
查看>>
Objective-C实现Http Post请求(附完整源码)
查看>>
Objective-C实现http下载文件 (附完整源码)
查看>>
Objective-C实现Http协议下载文件(附完整源码)
查看>>
Objective-C实现ID3贪心算法(附完整源码)
查看>>
Objective-C实现IIR 滤波器算法(附完整源码)
查看>>
Objective-C实现IIR数字滤波器(附完整源码)
查看>>
Objective-C实现insertion sort插入排序算法(附完整源码)
查看>>
Objective-C实现integer partition整数分区算法(附完整源码)
查看>>
Objective-C实现integerPartition整数划分算法(附完整源码)
查看>>