views:

64

answers:

1

I have a LINQ expression that gets used as a filer in a LINQ to SQL statement where clause. My problem is that the LINQ to SQL expression has become unwieldy and also the logic it contains has ended up in multiple locations violating DRY (this is the 3rd time I'm working on a bug raised by QA for it getting out of sync).

Is there any way I can reuse a LINQ expression like the one below or decompose it into smaller sub-expressions? My biggest issue is that because the expression is used in a LINQ to SQL call it cannot use any other classes or methods since the LINQ to SQL library doesn't won't know how to express them as SQL.

public static Expression<Func<MyClass, bool>> MyClassFilterExpression
{
    get
    {
        return x => (x.Status.Count > 0 && x.Status.Any(y => y.StatusID == 5)) ? "Refused" :
            (x.Status.Count > 0 && x.Status.Any(y => y.StatusID == 6)) ? "Cancelled" :
                (x.DateTimeExpired.HasValue && DateTime.Today > x.DateTimeExpired.Value) ? "Expired" :
                    (x.Duration.HasValue && x.DurationTypeID.HasValue && x.DateTimeApproved.HasValue) ?
                        (x.DurationTypeID == (int)DurationTypes.Day && DateTime.Today > x.DateTimeApproved.Value.AddDays(x.Duration.Value)) ? "Expired" :
                            (x.Status.Count > 0 && x.Status.Any(y => y.StatusID == 4 || y.StatusID == 10)) ? "Approved" : "Pending").Contains(filterValue);
    }
}

Ideally what I'd like to be able to do is something like the below. It doesn't work because LINQ to SQL can't express the GetStatus() method. Hopefully there is some other clever way to do it?

public ReviewStatuses GetStatus(DateTime? dateTimeExpired, int? reviewStatusID)
{
    var isExpired = (dateTimeExpired.HasValue && DateTime.Today >= dateTimeExpired.Value.Date);

    if (reviewStatusID == ReviewStatuses.Cancelled.GetHashCode())
    {
        return ReviewStatuses.Cancelled;
    }
    else if (reviewStatusID == ReviewStatuses.Refused.GetHashCode())
    {
        return ReviewStatuses.Refused;
    }
    return ReviewStatuses.Pending;
}

public static Expression<Func<MyClass, bool>> MyClassFilterExpression
{
    get
    {
        return x => x.Status.Count > 0 && x.Status.Any(y => GetStatus(y.DateTimeExpired, y.StatusID)).Contains(filterValue);
    }
}
+1  A: 

Try something like the following:

public static Expression<Func<MyClass, bool>> GetMyClassFilterExpression(string filterValue) {

  if (filterValue == "Refused")
    return x => (x.Status.Count > 0 && x.Status.Any(y => y.StatusID == 5));
  else if (filterValue == "Cancelled")
    return x => (x.Status.Count > 0 && x.Status.Any(y => y.StatusID == 6));
  else if (filterValue == "Expired")
    return x => (x.DateTimeExpired.HasValue && DateTime.Today > x.DateTimeExpired.Value) ||
      (x.DurationTypeID == (int)DurationTypes.Day && DateTime.Today > x.DateTimeApproved.Value.AddDays(x.Duration.Value));
  else if (filterValue == "Approved")
    return x => (x.Status.Count > 0 && x.Status.Any(y => y.StatusID == 4 || y.StatusID == 10));
  // and so on...
  else
    throw new ArgumentException("Your message.");
}

....

string filterValue = "Refused";
Expression<Func<MyClass, bool>> whereCluase = GetMyClassFilterExpression(filterValue);
var list = dataContext.MyClasses.Where(whereCluase).ToList();
Devart
You can `switch(string)`
abatishchev