LINQ SingleOrDefault方法

本文概述

  • LINQ SingleOrDefault()方法的语法
  • LINQ SingleOrDefault()方法的示例
【LINQ SingleOrDefault方法】在LINQ中, SingleOrDefault()方法用于返回单个元素。如果列表/集合中没有元素, 则它将返回多个元素, 并会引发异常, 如Single()方法。
LINQ SingleOrDefault()方法的语法 这是使用LINQ SingleOrDefault()方法从集合中获取单个元素的语法。
int a = objList.SingleOrDefault();

在以上语法中, 我们将从集合” objList” 中返回单个元素。并且, 如果集合不包含任何元素, 则它将返回默认值。
LINQ SingleOrDefault()方法的示例 这是LINQ SingleOrDefault方法的示例, 当元素不存在于集合中时, 该方法可从集合中获取单个元素。
using System; using System. Collections; using System.Collections.Generic; using System. Linq; using System. Text; using System.Threading.Tasks; namespace ConsoleApp1{class Programme2{static void Main(string[] args){//create the object 'objStudnet' of the class 'Student' added the record to the list List< Student> objStudent = new List< Student> (){new Student() { Name = "Akshay Tyagi", Gender = "Male", Location="Chennai" }, new Student() { Name = "Vaishali Tyagi", Gender = "Female", Location="Chennai" }, new Student() { Name = "Arpita Rai", Gender = "Male", Location="Bangalore" }, new Student() { Name = "Shubham Rastogi", Gender = "Male", Location ="Vizag"}, new Student() { Name = "Aman Singhal", Gender = "Male", Location="Nagpur"}}; //initialize the array 'vs' from 1 to 5int[] vs = { 1, 2, 3, 4, 5 }; //objStudent.SingleOrDefault() method will return the information of the studentvar user = objStudent.SingleOrDefault(i => i.Name == "Akshay Tyagi"); string result = user.Name; int val = vs.SingleOrDefault(j => j > 5); Console.WriteLine("Element from objStudent: {0}", result); Console.WriteLine("Element from objList: {0}", val); Console.ReadLine(); }}class Student{public string Name { get; set; }public string Gender { get; set; }public string Location { get; set; }}}

在上面的示例中, 我们尝试使用LINQ SingleOrDefault()方法从两个列表(objStudent, objList)对象中获取单个元素
输出
LINQ SingleOrDefault方法

文章图片
如果列表/集合返回多个元素, 则LINQ SingleOrDefault()方法将引发InvalidOperationException错误。
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1{class Programme2{static void Main(string[] args){List< Student> objStudent = new List< Student> (){new Student() { Name = "Akshay Tyagi", Gender = "Male", Location="Chennai" }, new Student() { Name = "Vaishali Tyagi", Gender = "Female", Location="Chennai" }, new Student() { Name = "Arpita Rai", Gender = "Male", Location="Bangalore" }, new Student() { Name = "Shubham Rastogi", Gender = "Male", Location ="Vizag"}, new Student() { Name = "Aman Singhal", Gender = "Male", Location="Nagpur"}}; //initialize the objList array from 1 to 5int[] objList = { 1, 2, 3, 4, 5 }; //here SingleOrDefault()method will return the default valueint val = objList.SingleOrDefault(); Console.WriteLine("Element from objList: {0}", val); Console.ReadLine(); //int[] vs = { 1, 2, 3, 4, 5 }; //var user = objStudent.SingleOrDefault(i => i.Name == "Akshay Tyagi"); //string result = user.Name; //int val = vs.SingleOrDefault(j => j > 5); //Console.WriteLine("Element from objStudent: {0}", result); //Console.WriteLine("Element from objList: {0}", val); Console.ReadLine(); }}class Student{public string Name { get; set; }public string Gender { get; set; }public string Location { get; set; }}}

如果我们运行上面的代码, 它将抛出InvalidOperationException错误, 因为” objlist” 返回了多个值。
输出
LINQ SingleOrDefault方法

文章图片

    推荐阅读