无涯教程-OrderBy运算符

39 阅读1分钟

在LINQ中,OrderBy运算符用于按升序对列表/集合值进行排序。在LINQ中,如果我们默认使用ORDER BY运算符,它将按升序对值列表进行排序。我们不需要在查询语句中添加任何升序条件。

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; } } }

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."

输出

LINQ OrderBy Operator(Ascending)




参考链接

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