您的OrderBy
条件应该为您的结果排序,您是否也想对结果进行分组,以便以A
、B
等开头的结果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; }
}
}
以上将为我提供一个按属性排序List
的Supplier
对象,然后按`第一个字符;Supplier1
Supplier
grouped
.GroupBy(c => c.Supplier1.Substring(0, 1))
该Supplier1
属性给了我五个元素到 my List
,每个元素也是有序的。