📜  LINQ OrderBy运算符(升序)

📅  最后修改于: 2021-01-06 05:13:34             🧑  作者: Mango

LINQ OrderBy运算符(升序)

在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 Objstudent = new List(){
        new Student() { Name = "Suresh Dasari", Gender = "Male", Subjects = new List { "Mathematics", "Physics" } },
        new Student() { Name = "Rohini Alavala", Gender = "Female", Subjects = new List { "Entomology", "Botany" } },
        new Student() { Name = "Praveen Kumar", Gender = "Male", Subjects = new List { "Computers", "Operating System", "Java" } },
        new Student() { Name = "Sateesh Chandra", Gender = "Male", Subjects = new List { "English", "Social Studies", "Chemistry" } },
        new Student() { Name = "Madhav Sai", Gender = "Male", Subjects = new List { "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 Subjects { get; set; }
    }
}

在上面的例子中,我们由学生收集条款声明类型var和使用的顺序可变学生姓名,并提到一个名为“”,以按升序排序,这是基于值的列表中选择列“名称”。

输出量