public static IQueryable ApplyEqualityFilter(IQueryable source, string propertyName, object value) { ParameterExpression parameterExpression = Expression.Parameter(source.ElementType, String.Empty); Expression propertyExpression = CreatePropertyExpression(parameterExpression, propertyName); if (Nullable.GetUnderlyingType(propertyExpression.Type) != null && value != null) { propertyExpression = Expression.Convert(propertyExpression, Misc.RemoveNullableFromType(propertyExpression.Type)); } value = Misc.ChangeType(value, propertyExpression.Type); Expression compareExpression = Expression.Equal(propertyExpression, Expression.Constant(value)); LambdaExpression lambda = Expression.Lambda(compareExpression, parameterExpression); MethodCallExpression whereCall = Expression.Call(typeof(Queryable), "Where", new Type[] { source.ElementType }, source.Expression, Expression.Quote(lambda)); return(source.Provider.CreateQuery(whereCall)); }
/// <summary> /// Look for this table's primary key in the route values (i.e. typically the query string). /// If they're all found, return a DataKey containing the primary key values. Otherwise return null. /// </summary> public DataKey GetDataKeyFromRoute() { var queryStringKeys = new OrderedDictionary(PrimaryKeyNames.Length); foreach (MetaColumn key in PrimaryKeyColumns) { // Try to find the PK in the route values. If any PK is not found, return null string value = Misc.GetRouteValue(key.Name); if (string.IsNullOrEmpty(value)) { return(null); } queryStringKeys[key.Name] = Misc.ChangeType(value, key.ColumnType); } return(new DataKey(queryStringKeys, PrimaryKeyNames)); }