📜  LINQ OrderBy降序运算符

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

LINQ OrderBy降序运算符

在LINQ中,OrderBy降序运算符用于按降序对项目的列表/集合进行排序。

LINQ OrderByDescending运算符的语法

LINQ中OrdrByDescending运算符的语法以降序对项目/集合列表进行排序:

C#代码

var studentname = Objstudent.OrderByDescending(x => x.Name);

LINQ OrderByDescending运算符示例

这是使用LINQ OrderByDescending排序运算符以降序对项目/集合列表进行排序的示例:

C#代码

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)
        {
//create object of Student class and create a list of the student information
            List Objstudent = new List()
            {
                new Student() { Name = "Akshay", Gender = "Male", Subjects = new List { "Mathematics", "Physics" } },
                new Student() { Name = "Vaishali", Gender = "Female", Subjects = new List { "Computer", "Botany" } },
                new Student() { Name = "Arpita", Gender = "FMale", Subjects = new List { "Economics", "Operating System", "Java" } },
                new Student() { Name = "Shubham", Gender = "Male", Subjects = new List { "Account", "Social Studies", "Chemistry" } },
                new Student() { Name = "Himanshu", Gender = "Male", Subjects = new List{ "English", "Charted" } }
    };
    /*OrderByDescending() operator is used to print 
    the name of the student in the descending form*/
    var studentname = Objstudent.OrderByDescending(x => x.Name);
    //foreach loop is used to print the name of the student
    foreach (var student in studentname)
    {
    Console.WriteLine(student.Name);
    }
    Console.ReadLine();
    }
    }
    //create a class student
    class Student
    {
    public string Name { get; set; }
    public string Gender { get; set; }
    public List
        Subjects { get; set; }
        }
        }

以下是上述代码按降序排列的结果:

输出: