1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
namespace System.Web.UI.WebControls.Expressions {
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Resources;
using System.Web.UI;
using System.Web.UI.WebControls;
public class RangeExpression : ParameterDataSourceExpression {
public string DataField {
get {
return (string)ViewState["DataField"] ?? String.Empty;
}
set {
ViewState["DataField"] = value;
}
}
public RangeType MinType {
get {
object o = ViewState["MinType"];
return o != null ? (RangeType)o : RangeType.None;
}
set {
ViewState["MinType"] = value;
}
}
public RangeType MaxType {
get {
object o = ViewState["MaxType"];
return o != null ? (RangeType)o : RangeType.None;
}
set {
ViewState["MaxType"] = value;
}
}
internal virtual new IOrderedDictionary GetValues() {
return Parameters.GetValues(Context, Owner);
}
public override IQueryable GetQueryable(IQueryable source) {
if (source == null) {
return null;
}
if (String.IsNullOrEmpty(DataField)) {
throw new InvalidOperationException(AtlasWeb.Expressions_DataFieldRequired);
}
IOrderedDictionary values = GetValues();
ParameterExpression parameterExpression = Expression.Parameter(source.ElementType, String.Empty);
Expression properyExpression = ExpressionHelper.GetValue(ExpressionHelper.CreatePropertyExpression(parameterExpression, DataField));
if (MinType == RangeType.None && MaxType == RangeType.None) {
throw new InvalidOperationException(AtlasWeb.RangeExpression_RangeTypeMustBeSpecified);
}
Expression minExpression = null;
Expression maxExpression = null;
if (MinType != RangeType.None) {
if (values.Count == 0) {
throw new InvalidOperationException(AtlasWeb.RangeExpression_MinimumValueRequired);
}
if (values[0] != null) {
minExpression = GetMinRangeExpression(properyExpression, values[0], MinType);
}
}
if (MaxType != RangeType.None) {
if (values.Count == 0 || ((minExpression != null) && (values.Count == 1))) {
throw new InvalidOperationException(AtlasWeb.RangeExpression_MaximumValueRequired);
}
object maxValue = minExpression == null ? values[0] : values[1];
if (maxValue != null) {
maxExpression = GetMaxRangeExpression(properyExpression, maxValue, MaxType);
}
}
if ((maxExpression == null) && (minExpression == null)) {
return null;
}
Expression rangeExpression = CreateRangeExpressionBody(minExpression, maxExpression);
return ExpressionHelper.Where(source, Expression.Lambda(rangeExpression, parameterExpression));
}
private static Expression GetMinRangeExpression(Expression propertyExpression, object value, RangeType rangeType) {
ConstantExpression constantValue = Expression.Constant(ExpressionHelper.BuildObjectValue(value, propertyExpression.Type));
switch (rangeType) {
case RangeType.Exclusive:
return Expression.GreaterThan(propertyExpression, constantValue);
case RangeType.None:
return null;
case RangeType.Inclusive:
return Expression.GreaterThanOrEqual(propertyExpression, constantValue);
default:
Debug.Fail("shouldn't get here!");
return null;
}
}
private static Expression GetMaxRangeExpression(Expression propertyExpression, object value, RangeType rangeType) {
ConstantExpression constantValue = Expression.Constant(ExpressionHelper.BuildObjectValue(value, propertyExpression.Type));
switch (rangeType) {
case RangeType.Exclusive:
return Expression.LessThan(propertyExpression, constantValue);
case RangeType.None:
return null;
case RangeType.Inclusive:
return Expression.LessThanOrEqual(propertyExpression, constantValue);
default:
Debug.Fail("shouldn't get here!");
return null;
}
}
private static Expression CreateRangeExpressionBody(Expression minExpression, Expression maxExpression) {
if (minExpression == null && maxExpression == null) {
return null;
}
if (minExpression == null) {
return maxExpression;
}
if (maxExpression == null) {
return minExpression;
}
return Expression.AndAlso(minExpression, maxExpression);
}
}
}
|