有没有一种方法可以使linq内部的条件可配置?

时间:2019-05-21 15:13:57

标签: c# .net linq

我正在优化服务器端应用程序,我想将可选参数传递给方法,并在这些参数的函数中使用where子句。

方法1:


public static Lane GetLane(string laneCode)
    {
        AppContext.TraceLogger.VerboseIf(MethodBase.GetCurrentMethod().Name, traceSwitch);
        try
        {

            using (GFC_Entities connexionEF = new GFC_Entities())
            {
                Lane lane = connexionEF.Lanes.Where(ln => ln.ShortDescription.Trim() == laneCode.Trim()).FirstOrDefault();
                CacheManager.AddToCache(cache_key, lane);
                return lane;
            }
        }
        catch (EntityException ex)
        {
            string message = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
            throw new TechnicalException(MethodBase.GetCurrentMethod().DeclaringType.ToString(), string.Format("{0} [9001]", message), ex);
        }
    }

方法2:

public static Lane GetLaneWithIdentifier(int laneIdentifier)
   {
        AppContext.TraceLogger.VerboseIf(MethodBase.GetCurrentMethod().Name, traceSwitch);

        try
        {
            using (GFC_Entities connexionEF = new GFC_Entities())
            {
                return connexionEF.Lanes.Where(ln => ln.Identifier == laneIdentifier).FirstOrDefault();
            }
        }
        catch (EntityException ex)
        {
            string message = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
            throw new TechnicalException(MethodBase.GetCurrentMethod().DeclaringType.ToString(), string.Format("{0} [9001]", message), ex);
        }
    }

我想像这样:

public static Lane GetLane(int laneIdentifier = 0, string laenCode ="")

和where子句取决于我传递的参数。谢谢。

1 个答案:

答案 0 :(得分:0)

您可以通过使用两个值的单个linq表达式来实现。

public static Lane GetLaneWithIdentifier(int laneIdentifier = 0, string laenCode = "")
{
    AppContext.TraceLogger.VerboseIf(MethodBase.GetCurrentMethod().Name, traceSwitch);

    try
    {
        using (GFC_Entities connexionEF = new GFC_Entities())
        {
            //If laneIdentifer is not equal to 0 it will evaluate the comparison,
            //Or if laenCode is not an empty string, it will evaluate that expression
            return connexionEF.Lanes.Where(ln => (laneIdentifier == 0 || ln.Identifier == laneIdentifier) 
                || (laenCode == "" || ln.ShortDescription.Trim() == laenCode)).FirstOrDefault();
        }
    }
    catch (EntityException ex)
    {
        string message = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
        throw new TechnicalException(MethodBase.GetCurrentMethod().DeclaringType.ToString(), string.Format("{0} [9001]", message), ex);
    }
}

另一个选择是像这样执行之前动态创建where子句

public static Lane GetLaneWithIdentifier(int laneIdentifier = 0, string laenCode = "")
{
    AppContext.TraceLogger.VerboseIf(MethodBase.GetCurrentMethod().Name, traceSwitch);

    try
    {
        using (GFC_Entities connexionEF = new GFC_Entities())
        {
            Expression<Func<Lane>> predicate = laneIdentifier != 0 
                ? c => c.Identifier == laneIdentifier 
                : c => c.ShortDescription.Trim() == laenCode;
            return connexionEF.Lanes.Where(predicate).FirstOrDefault();
        }
    }
    catch (EntityException ex)
    {
        string message = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
        throw new TechnicalException(MethodBase.GetCurrentMethod().DeclaringType.ToString(), string.Format("{0} [9001]", message), ex);
    }
}