0

I am working on the optimization of server side application, and i want to pass in optional parameters to a method and make where clause in function of those parameters.

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

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

I would like to make it like :

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

and the where clause depends on what parameter I pass. Thank you.

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
F.MANNOU
  • 23
  • 1
  • 7

1 Answers1

0

You can achieve this with a single linq expression using both values.

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

Another option is to dynamically create your where clause prior to executing it like so

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);
    }
}
hawkstrider
  • 4,141
  • 16
  • 27