有两种变量做选择语句的有效方法吗?

时间:2015-06-20 23:26:24

标签: c# linq

在我的C#代码中,我需要评估两个非空变量。我制定了一套 if-else if 语句,但在我看来它看起来很丑陋而且有点太草率,即使它是正确的。

我查看MSDN Library并仅查看基于单个变量的选择示例。

是否有更清洁,更紧凑的方法来实现相同的结果?

更新:我填写了代码以提供更多上下文。再看一下,也许我可以根据参数直接操作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);
    }
}

4 个答案:

答案 0 :(得分:4)

您可以在IQueryable上创建一个扩展方法,用于处理OrderByOrderByDescending的排序:

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);
    }
}

答案 1 :(得分:2)

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;
}

答案 2 :(得分:1)

我会使用三元运算符来制作更紧凑,更易于阅读的内容。

这也会删除一些布尔检查,因为它不会复制任何布尔检查。

    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);
        }
    }

答案 3 :(得分:0)

我的看法:

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。