我檢查的排序參數,構建了一堆if
聲明:我該如何改進這個排序代碼?
if (sortDirection == "ASC")
{
if (sortBy == "Id")
return customerList.OrderBy(x => x.Id).Skip(startIndex).Take(pageSize).ToList();
if (sortBy == "FirstName")
return customerList.OrderBy(x => x.FirstName).Skip(startIndex).Take(pageSize).ToList();
if (sortBy == "City")
return customerList.OrderBy(x => x.City).Skip(startIndex).Take(pageSize).ToList();
}
else
{
if (sortBy == "Id")
return customerList.OrderByDescending(x => x.Id).Skip(startIndex).Take(pageSize).ToList();
if (sortBy == "FirstName")
return customerList.OrderByDescending(x => x.FirstName).Skip(startIndex).Take(pageSize).ToList();
if (sortBy == "City")
return customerList.OrderByDescending(x => x.City).Skip(startIndex).Take(pageSize).ToList();
}
如何做得更好?
定義「更好」。更好的是什麼? – Oded
你想以什麼方式「改善」它?它是否按預期工作?它太慢了嗎?你不喜歡代碼的結構嗎?我們需要更多信息。 –
我會推薦使用LINQ組合。請參閱http://stackoverflow.com/questions/5881107/how-can-i-build-entity-framework-queries-dynamically/5882243#5882243 – Euphoric