在LINQ中,OrderBy运算符用于按升序对列表/集合值进行排序。在LINQ中,如果我们默认使用ORDER BY运算符,它将按升序对值列表进行排序。我们不需要在查询语句中添加任何升序条件。
LINQ中LINQ OrderBy运算符的语法,用于按升序对列表/集合值进行排序。
C#代码
var studentname = Objstudent.OrderBy(x => x.Name);
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; } } }
In the above example, we declared a variable student name of type var and used order by clause in the student collection and mentioned the column named "Name" to sort the list of values in ascending order, which is based on "Name."
输出
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)