OrderBy运算符

OrderBy运算符 首页 / LinQ入门教程 / OrderBy运算符

在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)




祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入浅出gRPC -〔李林锋〕

数据分析实战45讲 -〔陈旸〕

Elasticsearch核心技术与实战 -〔阮一鸣〕

Swift核心技术与实战 -〔张杰〕

如何看懂一幅画 -〔罗桂霞〕

深度学习推荐系统实战 -〔王喆〕

Web漏洞挖掘实战 -〔王昊天〕

现代React Web开发实战 -〔宋一玮〕

技术领导力实战笔记 2022 -〔TGO 鲲鹏会〕

好记忆不如烂笔头。留下您的足迹吧 :)