3

我希望能够从函数内的以下查询中提取条件语句。这是一个 linq to EF 查询。我该怎么做?有没有其他方法可以应用面向对象的原则(如打开/关闭)?

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

3 回答 3

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)

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

于 2013-09-10T07:28:31.897 回答
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 "";
    }
}
于 2013-09-10T19:04:41.947 回答
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 "";
    }
}

看到它在 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 "";
        }
    }
}
于 2013-09-10T07:29:45.103 回答