Application creates business part of query dynamically using commands like
if (!string.IsNullOrEmpty( Param.CustName ) )
q = q.Where( c=c.CustomerName==Param.CustName );
if (!string.IsNullOrEmpty( Param.CustCity ) )
q = q.Where( c=c.City==Param.CustCity );
I do'nt know a way to get generated sql as string.
So this approach requires to use string builder instead of this. Large parts
of application should use string concatenation to build business-logic
queries instead of DLinq.
I'm not sure is this good solution. So I'm searching for a way to create
extension method
IQueryable<TLessThanOrEqual<T>( IQueryable<Tthis, string c1, string c2,
object v1, object2, Type v1Type, Type v2Type )
or in general form
IQueryable<TLessThanOrEqual<T>( IQueryable<Tthis, string[] propertyName,
object[] propertyValue, Type[] propertyValueType)
which generates comparison query (c1,c2) <= (v1,v2)
I created single property extension method below but don't know how to
change it to use two properties.
Andrus.
// creates property <= value query.
public static IQueryable<TLessThanOrEqual<T>(this IQueryable<Tsource,
string property, object value, Type propertyType)
{
ParameterExpression param = Expression.Parameter(typeof(T),
"x");
Expression val;
val = Expression.Constant(value, propertyType);
Expression prop = Expression.Property(param, property);
BinaryExpression testExp = null;
if (propertyType == typeof(string))
{
Expression call = Expression.Call(
typeof(Microsoft.VisualBasic.CompilerServices.Oper ators).GetMethod("CompareString",
new[] { typeof(string), typeof(string), typeof(bool) }),
prop, val,
Expression.Constant(false, typeof(bool)));
testExp = LambdaExpression.LessThan(call,
Expression.Constant(1, typeof(int)));
}
else
{
testExp = LambdaExpression.LessThanOrEqual(prop, val);
}
return source.Where(Expression.Lambda<Func<T, bool>>(testExp,
param));
}
"Michel Walsh" <va*************************@nospam.comwrote in message
news:FD**********************************@microsof t.com...
In the same spirit, but more LINQ related, you can also use ExecuteQuery:
var query = dataContext.ExecuteQuery<className>( @"SELECT ... WHERE
... AND... OR... ");
where className is appropriate to recuperate the result of your
dynamically built SQL statement, as a string.
Vanderghast, Access MVP