0
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 的数据?

4

1 回答 1

0

您的OrderBy条件应该为您的结果排序,您是否也想对结果进行分组,以便以AB等开头的结果C放在一起?如果是这样,那么您可以使用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; }
    }
}

以上将为我提供一个按属性排序ListSupplier对象,然后按`第一个字符;Supplier1Suppliergrouped

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

Supplier1属性给了我五个元素到 my List,每个元素也是有序的。

于 2013-10-13T16:01:19.710 回答