471,338 Members | 1,295 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Re: convert query to DLinq

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
1 1997
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.

Similar topics

12 posts views Thread by John Scott | last post: by
7 posts views Thread by Senna | last post: by
reply views Thread by Andrus | last post: by
19 posts views Thread by Andrus | last post: by
4 posts views Thread by Andrus | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.