Application creates business part of query dynamically using commands like
if (!string.IsNull OrEmpty( Param.CustName ) )
q = q.Where( c=c.CustomerNam e==Param.CustNa me );
if (!string.IsNull OrEmpty( 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<TLes sThanOrEqual<T> ( IQueryable<Tthi s, string c1, string c2,
object v1, object2, Type v1Type, Type v2Type )
or in general form
IQueryable<TLes sThanOrEqual<T> ( IQueryable<Tthi s, string[] propertyName,
object[] propertyValue, Type[] propertyValueTy pe)
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<TLes sThanOrEqual<T> (this IQueryable<Tsou rce,
string property, object value, Type propertyType)
{
ParameterExpres sion param = Expression.Para meter(typeof(T) ,
"x");
Expression val;
val = Expression.Cons tant(value, propertyType);
Expression prop = Expression.Prop erty(param, property);
BinaryExpressio n testExp = null;
if (propertyType == typeof(string))
{
Expression call = Expression.Call (
typeof(Microsof t.VisualBasic.C ompilerServices .Operators).Get Method("Compare String",
new[] { typeof(string), typeof(string), typeof(bool) }),
prop, val,
Expression.Cons tant(false, typeof(bool)));
testExp = LambdaExpressio n.LessThan(call ,
Expression.Cons tant(1, typeof(int)));
}
else
{
testExp = LambdaExpressio n.LessThanOrEqu al(prop, val);
}
return source.Where(Ex pression.Lambda <Func<T, bool>>(testExp,
param));
}
"Michel Walsh" <va************ *************@n ospam.comwrote in message
news:FD******** *************** ***********@mic rosoft.com...
In the same spirit, but more LINQ related, you can also use ExecuteQuery:
var query = dataContext.Exe cuteQuery<class Name>( @"SELECT ... WHERE
... AND... OR... ");
where className is appropriate to recuperate the result of your
dynamically built SQL statement, as a string.
Vanderghast, Access MVP