在 asp.net 中以字母形式对数据进行排序

本文关键字:数据 排序 net asp | 更新日期: 2023-09-27 18:35:21

protected List<supplier> GetPartner()
{
    supplierDAL supDAL = new supplierDAL();
    List<supplier> list = supDAL.FindByCondition(c => c.status.Equals(true))
                                .OrderBy(c => c.supplier1)
                                .ToList();
    return list;
}
A: a1 a2

a3, B: b1 b2 b3, C: c1 c2 c3

如何将数据排序为

字母表,数据排序为 sql ?

在 asp.net 中以字母形式对数据进行排序

您的OrderBy条件应该对结果进行排序,您是否也想对结果进行分组,以便以ABC等开头的结果在一起? 如果是这样,那么您可以使用 GroupBy ,例如:

namespace ConsoleApplication
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    class Program
    {
        static void Main(string[] args)
        {
            var sourceList = new List<Supplier>()
            {
                new Supplier() {Status = true, Supplier1 = "Z99"},
                new Supplier() {Status = true, Supplier1 = "F32"},
                new Supplier() {Status = false, Supplier1 = "B2"},
                new Supplier() {Status = true, Supplier1 = "C3"},
                new Supplier() {Status = true, Supplier1 = "B1"},
                new Supplier() {Status = true, Supplier1 = "A33"},
                new Supplier() {Status = true, Supplier1 = "A3"},
                new Supplier() {Status = true, Supplier1 = "C1"},
            };
            var list = sourceList.Where(c => c.Status.Equals(true)).OrderBy(c => c.Supplier1).GroupBy(c => c.Supplier1.Substring(0, 1)).ToList();
            Console.ReadLine();
        }
   }
    public class Supplier
    {
        public bool Status { get; set; }
        public string Supplier1 { get; set; }
    }
}

以上内容将为我提供Supplier对象的List,这些对象按SupplierSupplier1属性排序,然后按"第一个字符"进行grouped;

.GroupBy(c => c.Supplier1.Substring(0, 1))

Supplier1属性中,我List有五个元素,每个元素也是有序的。