【问题标题】:sort list of students by name in c# [closed]在c#中按姓名对学生列表进行排序[关闭]
【发布时间】:2020-05-08 16:02:37
【问题描述】:

我无法按StudentName 的字母顺序对学生进行排序。

List<Student> studentList = new List<Student>() { 
                new Student() { StudentID = 1, StudentName = "John", Age = 18, Country = "Poland"  } ,
                new Student() { StudentID = 2, StudentName = "Steve",  Age = 22, Country = "Poland"  } ,
                new Student() { StudentID = 3, StudentName = "Bill",  Age = 18, Country = "USA"  } ,
                new Student() { StudentID = 4, StudentName = "Ram" , Age = 20, Country = "USA"  } ,
                new Student() { StudentID = 5, StudentName = "Ron" , Age = 21, Country = "Germany"  } 
            };

var listAlphabetically = studentList.OrderBy(x=>x.StudentName);  
            Console.WriteLine(listAlphabetically);

【问题讨论】:

  • 有什么问题?看起来你排序很好,但你不知道如何输出结果?
  • 您希望看到Console.WriteLine(listAlphabetically) 什么? Console.WriteLineobject 上调用该对象的 .ToString() 实现 - 这对大多数对象没有直接帮助。
  • 只要把输出改成Console.WriteLine(string.Join(",", listAlphabetically));
  • 您真正要问的是“如何在控制台中显示一系列项目?”

标签: c# list sorting alphabetical


【解决方案1】:

目前您正在尝试使用Console.WriteLine() 将序列对象listAlphabetically 直接打印到控制台。

var listAlphabetically = studentList.OrderBy(x => x.StudentName);  
Console.WriteLine(listAlphabetically);

Console.WriteLine() 在内部调用对象.ToString()-方法。该对象被转换为它的string-representation,然后将打印到控制台。在您的情况下,这可能看起来像这样:System.Linq.OrderedEnumerable2[StackOverflow.Program+Student,System.String]

要覆盖对象string-representation,您需要自己定义.ToString() 方法。因此,一个示例可能如下所示:

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public int Age { get; set; }
    public string Country { get; set; }

    public override string ToString()
    {
        return $"StudentID: {this.StudentID} - StudentName: {this.StudentName} - Age: {this.Age} - Country: {this.Country}";
    }
}

然后您将能够迭代Students 的序列并将它们的表示打印到控制台:

var listAlphabetically = studentList.OrderBy(x => x.StudentName);

// iterate students and print them to the console
foreach (var student in listAlphabetically)                   
    Console.WriteLine(student);

如果您只想将每个学生的单个属性打印到控制台,您可以执行以下操作:

var listAlphabetically = studentList.OrderBy(x => x.StudentName);

// iterate students and print only the ID of each student
foreach (var student in listAlphabetically)                   
    Console.WriteLine(student.StudentID);

完整示例

using System;
using System.Collections.Generic;
using System.Linq;

namespace StackOverflow
{
    public class Program
    {
        public class Student
        {
            public int StudentID { get; set; }
            public string StudentName { get; set; }
            public int Age { get; set; }
            public string Country { get; set; }

            public override string ToString()
            {
                return $"StudentID: {this.StudentID} - StudentName: {this.StudentName} - Age: {this.Age} - Country: {this.Country}";
            }
        }

        public static void Main(string[] args)
        {
            List<Student> studentList = new List<Student>() {
                new Student() { StudentID = 1, StudentName = "John", Age = 18, Country = "Poland"  } ,
                new Student() { StudentID = 2, StudentName = "Steve",  Age = 22, Country = "Poland"  } ,
                new Student() { StudentID = 3, StudentName = "Bill",  Age = 18, Country = "USA"  } ,
                new Student() { StudentID = 4, StudentName = "Ram" , Age = 20, Country = "USA"  } ,
                new Student() { StudentID = 5, StudentName = "Ron" , Age = 21, Country = "Germany"  }
            };

            var listAlphabetically = studentList.OrderBy(x => x.StudentName);

            // iterate students and print them to the console
            foreach (var student in listAlphabetically)
                Console.WriteLine(student);

            // keep console open
            Console.ReadKey();
        }
    }
}

注意

不要忘记导入System.Collections.GenericSystem.Linq namespace,因为您将需要它们来执行上述代码。

using System.Collections.Generic;
using System.Linq;

【讨论】:

    【解决方案2】:

    OrderBy 返回IOrderedEnumerable。你必须遍历它:

    var listAlphabetically = studentList.OrderBy(x => x.StudentName);
    
    foreach (var student in listAlphabetically)
    {
        Console.WriteLine($"Name: {student.StudentName}, Age: {student.Age}, Country: {student.Country}");
    }
    

    输出:

    Name: Bill, Age: 18, Country: USA
    Name: John, Age: 18, Country: Poland
    Name: Ram, Age: 20, Country: USA
    Name: Ron, Age: 21, Country: Germany
    Name: Steve, Age: 22, Country: Poland
    

    【讨论】:

      【解决方案3】:

      如果没有给出字符串,WriteLine 等函数将调用ToString()。其默认实现是打印类名。没有自动为您打印 Array 或 Enumerator 的内容。 (即使有,Student 类也可能没有覆盖ToString())。

      如果你想打印数组内容,你必须自己迭代它并调用适当的属性:

      foreach(Student current in studentList ){
          Console.WriteLine(current.StudentName);
      }
      

      当然,在您的情况下,您希望使用排序来迭代枚举器:

      foreach(Student current in listAlphabetically){
          Console.WriteLine(current.StudentName);
      }
      

      或使用其他人的循环,例如使用 James LINQ 加入解决方案。

      【讨论】:

      • 仅供参考,这不是一个数组。
      • @juharr 写完才发现,是个枚举数。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多