我是一名相当初级的C#开发人员,请原谅我这很容易。我在点击表格列标题时尝试按ASC
和DESC
顺序对数据进行排序,但出于某种原因,它没有这样做。
以下是您检查的分类代码:
if (sorting != null)
{
if (sorting.Equals("TrackID ASC"))
{
daa.OrderBy(p => p.TrackID);
}
else if (sorting.Equals("TrackID DESC"))
{
daa.OrderByDescending(p => p.TrackID);
}
if (sorting.Equals("TrackName ASC"))
{
daa.OrderBy(p => p.TrackName);
}
else if (sorting.Equals("TrackName DESC"))
{
daa.OrderByDescending(p => p.TrackName);
}
else if (sorting.Equals("DateTimes ASC"))
{
daa.OrderBy(p => p.Date);
}
else if (sorting.Equals("DateTimes DESC"))
{
daa.OrderByDescending(p => p.Date);
}
else if (sorting.Equals("ArtistName ASC"))
{
daa.OrderBy(p => p.ArtistName);
}
else if (sorting.Equals("ArtistName DESC"))
{
daa.OrderByDescending(p => p.ArtistName);
}
else if (sorting.Equals("Times ASC"))
{
daa.OrderBy(p => p.Times);
}
else if (sorting.Equals("Times DESC"))
{
daa.OrderByDescending(p => p.Times);
}
}
有人可以解释为什么它不起作用,我该如何解决并实现它以ASC
和DESC
顺序对数据进行排序?
任何帮助都会很棒:)谢谢
答案 0 :(得分:1)
如果我没记错,方法“OrderBy”和“OrderByDescending”不会对直接调用它们的对象进行排序。相反,他们返回IOrderedEnumerable<对象>集合所以你需要自己使用IOrderedEnumerable作为参考对daa集合进行排序,或者从IOrderedEnumerable创建集合类型并将其分配给daa。当然刷新ui。
答案 1 :(得分:1)
if (sorting != null)
{
if (sorting.Equals("TrackID ASC"))
{
daa = daa.OrderBy(p => p.TrackID).ToList();
}
else if (sorting.Equals("TrackID DESC"))
{
daa = daa.OrderByDescending(p => p.TrackID).ToList();
}
if (sorting.Equals("TrackName ASC"))
{
daa = daa.OrderBy(p => p.TrackName).ToList();
}
else if (sorting.Equals("TrackName DESC"))
{
daa = daa.OrderByDescending(p => p.TrackName).ToList();
}
else if (sorting.Equals("DateTimes ASC"))
{
daa = daa.OrderBy(p => p.Date).ToList();
}
else if (sorting.Equals("DateTimes DESC"))
{
daa = daa.OrderByDescending(p => p.Date).ToList();
}
else if (sorting.Equals("ArtistName ASC"))
{
daa = daa.OrderBy(p => p.ArtistName).ToList();
}
else if (sorting.Equals("ArtistName DESC"))
{
daa = daa.OrderByDescending(p => p.ArtistName).ToList();
}
else if (sorting.Equals("Times ASC"))
{
daa = daa.OrderBy(p => p.Times).ToList();
}
else if (sorting.Equals("Times DESC"))
{
daa = daa.OrderByDescending(p => p.Times).ToList();
}
}
尝试一次。