有没有一种有效的方法来执行具有两个变量的选择语句
本文关键字:变量 语句 选择 两个 一种 有效 有没有 方法 执行 | 更新日期: 2023-09-27 17:57:13
在我的 C# 代码中,我需要计算两个非空变量。我制定了一组if-else if语句,但在我看来,它看起来很丑,而且有点太草率了,即使它是正确的。
我查看了 MSDN 库,只看到了基于单个变量进行选择的示例。
有没有一种更清洁、更紧凑的方式来达到相同的结果?
更新:我填写了代码以提供更多上下文。 再看这个,也许我可以直接根据参数操作 linq 查询。但是,我提出的问题是我想关注的通用问题:选择而不是选择后使用的代码。
public ActionResult Index(string searchBy, string orderBy, string orderDir)
{
var query = fca.GetResultsByFilter(searchBy);
if (orderBy == "Campus" && orderDir == "Asc")
{
query = query = query.OrderBy(s => s.Campus).ThenBy(s => s.Student_Name);
}
else if (orderBy == "Campus" && orderDir == "Desc")
{
query = query.OrderByDescending(s => s.Campus);
}
else if (orderBy == "Student Name" && orderDir == "Asc")
{
query = query = query.OrderBy(s => s.Student_Name);
}
else if (orderBy == "Student Name" && orderDir == "Desc")
{
query = query.OrderByDescending(s => s.Student_Name);
}
else if (orderBy == "Course Count" && orderDir == "Asc")
{
query = query.OrderBy(s => s.Course_Count);
}
else if (orderBy == "Course Count" && orderDir == "Desc")
{
query = query.OrderByDescending(s => s.Course_Count);
}
}
您可以在IQueryable
上创建一个扩展方法,该方法处理使用 OrderBy
或 OrderByDescending
的排序:
public static class QueryableExtensions
{
public static IOrderedQueryable<TSource> OrderByWithDirection<TSource,TKey>
(this IQueryable<TSource> source,
Expression<Func<TSource, TKey>> keySelector,
string orderDir)
{
return orderDir == "Desc"
? source.OrderByDescending(keySelector)
: source.OrderBy(keySelector);
}
}
我假设您的GetResultsByFilter
方法返回一个IQueryable<>
.如果它实际上返回了一个IEnumerable<>
,那么扩展方法将需要接受一个IEnumerable<TSource> source
参数并返回一个IOrderedEnumerable<TSource>
。
然后可以按如下方式使用:
public ActionResult Index(string searchBy, string orderBy, string orderDir)
{
var query = fca.GetResultsByFilter(searchBy);
switch (orderBy)
{
case "Campus":
query = query.OrderByWithDirection(s => s.Campus, orderDir);
break;
case "Student Name":
query = query.OrderByWithDirection(s => s.Student_Name, orderDir);
break;
case "Course Count":
query = query.OrderByWithDirection(s => s.Course_Count, orderDir);
break;
}
if (orderBy == "Campus" && orderDir == "Asc")
{
// The Campus Asc case was also ordered by Student_Name in the question.
query = query.ThenBy(s => s.Student_Name);
}
}
CNot 确定这是否更好,只是不同。
switch (orderDir)
{
case "Asc":
Switch (orderBy)
{
case "Campus":
//Code here for Campus orderBy and Asc orderDir
break;
case "Student Name":
//Code here for Student Name orderBy and Asc orderDir
break;
case "Course Count":
//Code here for Course Count orderBy and Asc orderDir
break;
}
break;
case "Desc":
Switch (orderBy)
{
case "Campus":
//Code here for Campus orderBy and Desc orderDir
break;
case "Student Name":
//Code here for Student Name orderBy and Desc orderDir
break;
case "Course Count":
//Code here for Course Count orderBy and Desc orderDir
break;
}
break;
}
我会使用三元运算符来使这样更紧凑、更易于阅读。
这也将减少一些布尔检查,因为它不会重复其中任何一个。
public ActionResult Index(string searchBy, string orderBy, string orderDir)
{
var query = fca.GetResultsByFilter(searchBy);
if (orderBy == "Campus")
{
query = (orderDir == "Asc") ? query.OrderBy(s => s.Campus).ThenBy(s => s.Student_Name) :
query.OrderByDescending(s => s.Campus);
}
else if (orderBy == "Student Name")
{
query = (orderDir == "Asc") ? query.OrderBy(s => s.Student_Name) : query.OrderByDescending(s => s.Student_Name);
}
else if (orderBy == "Course Count")
{
query = (orderDir == "Asc") ? query.OrderBy(s => s.Student_Name) : query.OrderByDescending(s => s.Course_Count);
}
}
我的看法:
public interface IOrder {
void perform(Query query)
}
public abstract class AbstractOrder : IOrder {
protected string orderString;
public AbstractOrder(string orderString) {
this.orderString = orderString;
}
}
public class OrderAsc {
public OrderAsc(string orderString) : base(orderString) {
}
public Query perform(Query query) {
query = query.OrderBy(s => s.Course_Count); //here you still have to do a mapping between orderString and your db field s.Course_count
return query;
}
}
public class OrderDesc {
public OrderDesc(string orderString) : base(orderString) {
}
public Query perform(Query query) {
query = query.OrderByDescending(s => s.Course_Count); //here you still have to do a mapping between orderString and your db field s.Course_count, or maybe it's equal, then you can just replace it.
return query;
}
}
然后。。。
IList<IOrder> list = new List<IOrder>() {new OrderAsc("Campus"), new OrderDesc("Student Name")}
foreach(IOrder o in list) {
query = o.perform(query);
}
其中可能有一些错误,我手头没有 IDE 自动取款机。