0

如何对多列进行排序?我尝试使用这个表达式:

if (name != null)
{
    if (name.Equals(SortEnum.ASC))
    {
        employees = employees.OrderBy(e => e.Name);
    }
    else if (name.Equals(SortEnum.DESC))
    {
        employees = employees.OrderByDescending(e => e.Name);
    }
}

if (surname != null)
{
     if (surname.Equals(SortEnum.ASC))
     {
         employees = employees.OrderBy(e => e.Surname);
     }
     else if (surname.Equals(SortEnum.DESC))
     {
         employees = employees.OrderByDescending(e => e.Surname);
     }
}

但只有最后一列被排序。我在某个地方看到了 method ThenBy(),但我没有。请帮忙。

4

1 回答 1

0

使用System.Linq.Dynamic

List<string> orderstr = new List<string>();
orderstr.Add((name != null)? name.Equals(SortEnum.ASC)? "Name ASC": "Name DESC" : string.Empty);
orderstr.Add((surname != null)? surname.Equals(SortEnum.ASC)? "Surname ASC": "Surname DESC" : string.Empty);
var reslt = employees.OrderBy(string.Join(", ",orderstr.Where(x=>!string.IsNullOrEmpty(x))));
于 2013-08-25T09:47:04.307 回答