如何从linq查询中提取条件语句?

时间:2013-09-10 06:33:38

标签: c# asp.net-mvc linq linq-to-entities

我希望能够从函数内的后续查询中提取条件语句。它是EF查询的linq。我该怎么做?是否有任何替代方式,以便应用面向对象的原则(如打开/关闭)?

    var query = new UvwRequestAssignmentManagementBO().GetAll().Where(uvw => (uvw.FK_ProcessStep == 2)
        && (uvw.FK_Entity == SecurityContext.Current.User.FK_Entity)
       && (uvw.FK_Manager == 15))
       .Select(p => new ReqSupAdminGridVm()
                {
                    NameFamily = p.NameFamily,
                    RequestDate = p.RequestDate,
                    RequestNo = p.RequestNo,
                    RequestType = p.RequestType == 1 ?"a"
                                          : (p.RequestType == 2 ? "b"
                                          : (p.RequestType == 3 ? "c" :
                                          (p.RequestType == 4 ? "d" : ""))),
                    RequestEvaluationStatus = p.RequestEvaluationStatus_Aggregation == 1 ? "a"
                                          : (p.RequestEvaluationStatus_Aggregation == 2 ? "b"
                                          : (p.RequestEvaluationStatus_Aggregation == 3 ?"c" 
                                          :(p.RequestEvaluationStatus_Aggregation == 4 ? "d" : ""))), 
                });

例如而不是写作:

    RequestType = p.RequestType == 1 ?"a"
                                          : (p.RequestType == 2 ? "b"
                                          : (p.RequestType == 3 ? "c" :
                                          (p.RequestType == 4 ? "d" : ""))),

我希望能够在另一个类中写这个:

RequestType = ReqType.GetReqType(p.RequestType);

string GetReqType(int type){

           switch(type){
                      case 1:
                       return "a";
                      case 2:
                       return "b";
                          }
}

3 个答案:

答案 0 :(得分:2)

查看PredicateBuilder,它可以让你做我认为你问的问题,我在最近的一个项目中自己使用它,代码是可重用的,也是可读的。

我为我的模型创建了这样的谓词:

using System;
using System.Linq;
using System.Linq.Expressions;

namespace MyModels
{
    using Predicate = Expression<Func<Component, bool>>;

    public partial class Component
    {
        public static Predicate HasKeywordContaining(string keyword)
        {
            return c => c.Keywords.Any(k => k.Value.Contains(keyword));
        }

        public static Predicate IsOwnedBy(string ownerName)
        {
            return c => c.OwnerName.Contains(ownerName);
        }

        public static Predicate HasPartNoContaining(string partNo)
        {
            return c => c.PartNo.Contains(partNo);
        }
    }
}

然后我可以使用它们来构建这样的查询:

var whereComponent = PredicateBuilder.True<Component>();

whereComponent = whereComponent.And(Component.HasKeywordContaining(keyword));
whereComponent = whereComponent.And(Component.IsOwnedBy(ownerName));

var components = from c in db.Components.Where(whereComponent)

当然,这不涉及客户端过滤。

答案 1 :(得分:1)

在单独的类中创建扩展方法,如下所示:

public static class ReqTypeExtension
{
    public static string GetReqType(this int reqType)
    {
        switch (reqType)
        {
            case 1:
                return "a";
            case 2:
                return "b";
            case 3:
                return "c";
            case 4:
                return "d";
        }

        return "";
    }
}

像这样使用它:

RequestType = p.RequestType.GetReqType()

如果您使用以下语法设置:

RequestType = ReqType.GetReqType(p.RequestType);

在ReqType类上声明一个静态方法,如下所示:

public class ReqType {

    public static string GetReqType(int reqType)
    {
        switch (reqType)
        {
            case 1:
                return "a";
            case 2:
                return "b";
            case 3:
                return "c";
            case 4:
                return "d";
        }

        return "";
    }
}

答案 2 :(得分:0)

这应该有效

    var query = new UvwRequestAssignmentManagementBO().GetAll().Where(uvw => (uvw.FK_ProcessStep == 2)
        && (uvw.FK_Entity == 1)
        && (uvw.FK_Manager == 15))
        .Select(p => new ReqSupAdminGridVm
        {
            NameFamily = p.NameFamily,
            RequestDate = p.RequestDate,
            RequestNo = p.RequestNo,
            RequestType = GetReqType(p.RequestType),
            RequestEvaluationStatus = GetReqEvalStatus(p.RequestEvaluationStatus_Aggregation),
        });

如果添加以下本地帮助程序函数:

private static string GetReqEvalStatus(int agg)
{
    switch (agg)
    {
        case 1: return "a";
        case 2: return "b";
        case 3: return "c";
        case 4: return "d";
        default: return "";
    }
}

private static string GetReqType(int type)
{
    switch (type)
    {
        case 1: return "a";
        case 2: return "b";
        case 3: return "c";
        case 4: return "d";
        default: return "";
    }
}

看到它正常工作Live on IdeOne

如果需要,可以将辅助函数移动到另一个类/文件(http://ideone.com/EsK6W8)中:

            RequestType = ReqType.GetReqType(p.RequestType),
            RequestEvaluationStatus = ReqEvalStatus.GetReqEvalStatus(p.RequestEvaluationStatus_Aggregation),

在单独的文件中:

static internal class ReqEvalStatus
{
    public static string GetReqEvalStatus(int agg)
    {
        switch (agg)
        {
            case 1: return "a";
            case 2: return "b";
            case 3: return "c";
            case 4: return "d";
            default: return "";
        }
    }
}

static internal class ReqType
{
    public static string GetReqType(int type)
    {
        switch (type)
        {
            case 1: return "a";
            case 2: return "b";
            case 3: return "c";
            case 4: return "d";
            default: return "";
        }
    }
}