/// <summary>
/// Gets the Linq expression for the DataViewFilter.
/// </summary>
/// <param name="filteredEntityType">The object type of the filtered entity.</param>
/// <param name="serviceInstance">A <see cref="System.Object"/> that contains the service reference.</param>
/// <param name="parameter">A <see cref="System.Linq.Expressions.ParameterExpression"/> containing the parameter for the expression.</param>
/// <param name="errorMessages">A <see cref="System.Collections.Generic.List{String}"/> that contains any error/exception messages that are returned.</param>
/// <returns></returns>
public virtual Expression GetExpression(Type filteredEntityType, IService serviceInstance, ParameterExpression parameter, List <string> errorMessages)
{
switch (ExpressionType)
{
case FilterExpressionType.Filter:
if (this.EntityTypeId.HasValue)
{
var entityType = Rock.Web.Cache.EntityTypeCache.Read(this.EntityTypeId.Value);
if (entityType != null)
{
var component = Rock.Reporting.DataFilterContainer.GetComponent(entityType.Name);
if (component != null)
{
try
{
return(component.GetExpression(filteredEntityType, serviceInstance, parameter, this.Selection));
}
catch (SystemException ex)
{
ExceptionLogService.LogException(ex, System.Web.HttpContext.Current);
errorMessages.Add(string.Format("{0}: {1}", component.FormatSelection(filteredEntityType, this.Selection), ex.Message));
}
}
}
}
return(null);
case FilterExpressionType.GroupAll:
case FilterExpressionType.GroupAnyFalse:
Expression andExp = null;
foreach (var filter in this.ChildFilters)
{
Expression exp = filter.GetExpression(filteredEntityType, serviceInstance, parameter, errorMessages);
if (exp != null)
{
if (andExp == null)
{
andExp = exp;
}
else
{
andExp = Expression.AndAlso(andExp, exp);
}
}
}
if (ExpressionType == FilterExpressionType.GroupAnyFalse &&
andExp != null)
{
// If only one of the conditions must be false, invert the expression so that it becomes the logical equivalent of "NOT ALL".
andExp = Expression.Not(andExp);
}
return(andExp);
case FilterExpressionType.GroupAny:
case FilterExpressionType.GroupAllFalse:
Expression orExp = null;
foreach (var filter in this.ChildFilters)
{
Expression exp = filter.GetExpression(filteredEntityType, serviceInstance, parameter, errorMessages);
if (exp != null)
{
if (orExp == null)
{
orExp = exp;
}
else
{
orExp = Expression.OrElse(orExp, exp);
}
}
}
if (ExpressionType == FilterExpressionType.GroupAllFalse &&
orExp != null)
{
// If all of the conditions must be false, invert the expression so that it becomes the logical equivalent of "NOT ANY".
orExp = Expression.Not(orExp);
}
return(orExp);
}
return(null);
}