473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2159
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: John Scott | last post by:
Ok...here we go... I am trying to manually run an SQL server back up job from c#. Here is a snippet of my code to begin the process: string strSQL = "USE msdb " + "\n if exists(select * from...
2
by: Bubba | last post by:
I know it's possible, just don't know how to do it. I have a spreadsheet that I imported into access. Two of the columns in the table have Hard Drive space values listed for example 2.45 GB and 453...
7
by: Senna | last post by:
Hi Have a question about DLinq. The example code floating around looks like this: Northwind db = new Northwind(@"C:\...\northwnd.mdf"); var custs = from c in db.Customers where c.City ==...
0
by: Andrus | last post by:
I need to update natural primary key like Territories.TerritoryID column in northwind database. In pure ADO .NET this is easy. I can use ExecScalar to run UPDATE command on primary key. ...
0
by: Andrus | last post by:
How to create select columns list dynamically in DLinq ? I want to create something like Console.WriteLine("Enter list of columns to return, separated by commas:"); string list =...
19
by: Andrus | last post by:
I need to repeatedly execute same queries which returns single entity by id, like: Customer cust = (from c in db.Customers where c.CustomerID=="AIRBU" select c).SingleOrDefault(); DLinq...
9
by: Marc Gravell | last post by:
How to fix ? Write it the way that you know works... (i.e. the one you commented out), or write that parses the input string doing a Split on '.', and uses reflection to navigate the child...
11
by: Andrus | last post by:
I created dynamic extension methods for <= and < SQL comparison operators: public static IQueryable<TLessThanOrEqual<T>(this IQueryable<Tsource, string property, object value); public static...
4
by: Andrus | last post by:
DLinq objects: class Item { string ItemCode {get;set;} // primary key } class Stock { // primary key is (ItemCode,StockId) string ItemCode {get;set;} string Quantity { get; set; } string...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.