By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

Re: convert query to DLinq

P: n/a
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

Jul 2 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Jul 3 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.