本文概述
在LINQ中, OrderBy运算符用于按升序对列表/集合值进行排序。在LINQ中, 如果默认情况下我们使用运算符order, 则它将以升序对值列表进行排序。我们不需要在查询语句中添加任何升序条件。
LINQ OrderBy运算符的语法
LINQ中LINQ OrderBy运算符的语法以升序对列表/集合值进行排序。
C#代码
var studentname = Objstudent.OrderBy(x => x.Name);
LINQ OrderBy运算符示例
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<Student> Objstudent = new List<Student>(){
new Student() { Name = "Suresh Dasari", Gender = "Male", Subjects = new List<string> { "Mathematics", "Physics" } }, new Student() { Name = "Rohini Alavala", Gender = "Female", Subjects = new List<string> { "Entomology", "Botany" } }, new Student() { Name = "Praveen Kumar", Gender = "Male", Subjects = new List<string> { "Computers", "Operating System", "Java" } }, new Student() { Name = "Sateesh Chandra", Gender = "Male", Subjects = new List<string> { "English", "Social Studies", "Chemistry" } }, new Student() { Name = "Madhav Sai", Gender = "Male", Subjects = new List<string> { "Accounting", "Charted" } }
};
var studentname = Objstudent.OrderBy(x => x.Name);
foreach (var student in student name)
{
Console.WriteLine(student.Name);
}
Console.ReadLine();
}
}
class Student
{
public string Name { get; set; }
public string Gender { get; set; }
public List<string> Subjects { get; set; }
}
}
在上面的示例中, 我们声明了一个变量类型为var的学生姓名, 并在学生集合中使用了order by子句, 并提到了名为”名称”的列以基于”名称”的升序对值列表进行排序。
输出