gpt4 book ai didi

c# - 我怎样才能改进这个排序代码?

转载 作者:太空狗 更新时间:2023-10-29 21:09:54 24 4
gpt4 key购买 nike

<分区>

我正在检查排序参数并构建一堆 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();
}

我如何让它变得更好?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com