无涯教程-SingleOrDefault方法

60 阅读2分钟

在LINQ中,SingleOrDefault()方法用于返回单个元素。如果列表/集合中没有元素,则这将返回多个元素,并抛出类似单个()方法的异常。

LINQ SingleOrDefault()方法的语法

下面是使用LINQ SingleOrDefault()方法从集合中获取单个元素的语法。

  int a = objList.SingleOrDefault();

In the above syntax, we will return the single element from the collection "objList". And if the collection does not contain any element, then it will return the default value.

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 objStudent = new List() { 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 5 int[] vs = { 1, 2, 3, 4, 5 }; //objStudent.SingleOrDefault() method will return the information of the student 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; } } }

在上面的示例中,我们试图使用LINQSingleOrDefault()方法从两个列表(objStudents,objList)对象中获取单个元素

输出:

LINQ SingleOrDefault() Method

如果列表/集合返回多个元素,则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 objStudent = new List() { 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 5 int[] objList = { 1, 2, 3, 4, 5 }; //here SingleOrDefault()method will return the default value int 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; }

}

}

If we run the above code, it will throw the InvalidOperationException error because "objlist" returns the more than one value.

输出:

LINQ SingleOrDefault() Method




参考链接

www.learnfk.com/linq/linq-s…