|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Rewrite;
using System.Linq.Expressions;
using System.Data.Linq.SqlClient;
using RewriteSql;
namespace RewriteDemo {
class UserInput {
internal static PropertyInfo[] CustomerProps {
get {
// exclude EntitySets
return typeof( Customer ).GetProperties().Where( p => !p.PropertyType.IsGenericType ).ToArray();
}
}
internal static PropertyInfo[] CustomerExtProps {
// exclude EntitySets
get { return typeof( CustomerExt ).GetProperties().Where( p => !p.PropertyType.IsGenericType).ToArray(); }
}
internal static string[] CompareOps( Type type ) {
IEnumerable<string> ops = CompareOpDecoder.GetCompareOpNames( type );
if( ops != null )
return ops.ToArray();
return null;
}
public static Expression<Func<Customer, bool>> MakeCityFilter( string value ) {
if( string.IsNullOrEmpty( value ) )
return null; // ---------->>>>>>>>>>>>>
value = value.ToUpper();
Expression<Func<Customer, bool>> expr = c => SqlMethods.Like( c.City.ToUpper(), value );
// this is done solely for better readability
expr = SimpleRewriter.ApplyOnce( expr,
Rule.Create( () => value, EvaluateLiteral.CreateRhs( value ) ) );
return expr;
}
public static Expression<Func<Customer, bool>> MakeOrdersInYearFilter( string yearStr, string countStr ) {
int cnt, year;
if( !int.TryParse( countStr, out cnt ) || !int.TryParse( yearStr, out year ) )
return null; // ---------->>>>>>>>>>>>>
Expression<Func<Customer, bool>> expr
= c => c.Orders.Count( o => o.OrderDate.HasValue && o.OrderDate.Value.Year == year ) >= cnt;
var rwr = new SimpleRewriter( expr );
Expression<Func<int>> yearLhs = () => year;
rwr.ApplyOnce( new Rule( yearLhs, EvaluateLiteral.CreateRhs( typeof( int ), year ) ) );
Expression<Func<int>> cntLhs = () => cnt;
rwr.ApplyOnce( new Rule( cntLhs, EvaluateLiteral.CreateRhs( typeof( int ), cnt ) ) );
return (Expression<Func<Customer, bool>>)rwr.Expression;
}
public static Expression<Func<Customer, bool>> MakeGerericFilter( string propName, string opName, string value ) {
if( string.IsNullOrEmpty( propName) || string.IsNullOrEmpty( opName) || string.IsNullOrEmpty( value))
return null; // ---------->>>>>>>>>>>>>
return GenericBinFilter<Customer>.Create( propName, opName, value );
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.