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<Student> Objstudent = new List<Student>()
            {
                new Student() { Name = "Akshay", Gender = "Male", Subjects = new List<string> { "Mathematics", "Physics" } }, new Student() { Name = "Vaishali", Gender = "Female", Subjects = new List<string> { "Computer", "Botany" } }, new Student() { Name = "Arpita", Gender = "FMale", Subjects = new List<string> { "Economics", "Operating System", "Java" } }, new Student() { Name = "Shubham", Gender = "Male", Subjects = new List<string> { "Account", "Social Studies", "Chemistry" } }, new Student() { Name = "Himanshu", Gender = "Male", Subjects = new List<string>{ "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<string>
        Subjects { get; set; }
        }
        }

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

输出

LINQ OrderBy降序运算符

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?