473,399 Members | 2,858 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,399 software developers and data experts.

Dynamic Linq on Datatables

In my (Windows Forms) project I am using strongly typed datatables. Now I am
trying to convert my 'rowfilter/group by' query logic to Linq. Most queries I
can easily convert, but some I am having difficulties with: the ones dynamic
in nature. I solved it (temporary) by copy-pasting static queries. This is
not an ideal solution because now my code contains some duplicate code.

Some of the problems I have are:

- My select statements differ based on input from a dropdown. So for example
if a user selects from a dropdown 'A' the linq query must select 'ColomnX,
ColomnY, ColomnZ'. If he selects 'B' the query must select 'ColumnD,
ColumnE, ColumnZ'.

- Based on the number of active checkboxes from a CheckedListBox a 'Where'
statement must be dynamically created (just the same as you would do with a
SQL 'IN' statement). I tried something like:

for (int index = 0; index < checkedListBox.Item.Count; index++)
{
if (... Checked..)
{
query = query.Where(theStonglyTypedRow =theStonglyTypedRow .SomeColumn
== checkedListBox.Item[index] .Value)
}
}

But then I was caught by 'Deffered Execution' (my old construction was based
on DataView.RowFilter which worked fine this way)

Now I was looking around for some library and found in Scott Gu's blog a
reference to a Visual Studio 2008 example, '\LinqSamples\DynamicQuery'. No I
am having difficulties in getting it to work on datables because they seem to
implement the IQueryable interface. Does anybody have a working solution?

Thanks for any help!

Yours sincerely,

Evert Wiesenekker
Jan 28 '08 #1
9 14027
Sorry an important typo:
'they seem to implement the IQueryable interface' must be ' they seem NOT to
implement the IQueryable interface'
"Evert" wrote:
In my (Windows Forms) project I am using strongly typed datatables. Now I am
trying to convert my 'rowfilter/group by' query logic to Linq. Most queries I
can easily convert, but some I am having difficulties with: the ones dynamic
in nature. I solved it (temporary) by copy-pasting static queries. This is
not an ideal solution because now my code contains some duplicate code.

Some of the problems I have are:

- My select statements differ based on input from a dropdown. So for example
if a user selects from a dropdown 'A' the linq query must select 'ColomnX,
ColomnY, ColomnZ'. If he selects 'B' the query must select 'ColumnD,
ColumnE, ColumnZ'.

- Based on the number of active checkboxes from a CheckedListBox a 'Where'
statement must be dynamically created (just the same as you would do with a
SQL 'IN' statement). I tried something like:

for (int index = 0; index < checkedListBox.Item.Count; index++)
{
if (... Checked..)
{
query = query.Where(theStonglyTypedRow =theStonglyTypedRow .SomeColumn
== checkedListBox.Item[index] .Value)
}
}

But then I was caught by 'Deffered Execution' (my old construction was based
on DataView.RowFilter which worked fine this way)

Now I was looking around for some library and found in Scott Gu's blog a
reference to a Visual Studio 2008 example, '\LinqSamples\DynamicQuery'. No I
am having difficulties in getting it to work on datables because they seem to
implement the IQueryable interface. Does anybody have a working solution?

Thanks for any help!

Yours sincerely,

Evert Wiesenekker
Jan 28 '08 #2
btw, you can use .AsQueryable() to get any IEnumerable<Tto an
IQueryable<T- although in this case I think the IEnumerable<T>
should be fine (it should work pretty similarly either way... i.e.
using the LINQ-to-objects behaviour).

Marc
Jan 28 '08 #3
Is it mainly the "or" we are struggling with here? Suggest Union? (not
tested); "and" maps to another Where.

However - the code really doesn't make it clear what you are trying to
do and which bits are working (and which aren't) - however, if you
have it working with RowFilter, perhaps use it?

Marc
Jan 28 '08 #4
(generic Or/And)

static Func<T, boolOr<T>(this Func<T, boollhs, Func<T, bool>
rhs) {
return x =lhs(x) || rhs(x);
}
static Func<T, boolAnd<T>(this Func<T, boollhs, Func<T, bool>
rhs) {
return x =lhs(x) && rhs(x);
}
Jan 28 '08 #5
Or, since we know this is LINQ-to-objects (not full expression), can
simplify and simply combine the predicates?

Marc

static Func<int, boolOr(this Func<int, boollhs, Func<int,
boolrhs) {
return x =lhs(x) || rhs(x);
}
static Func<int, boolAnd(this Func<int, boollhs, Func<int,
boolrhs) {
return x =lhs(x) && rhs(x);
}
static void Main() {
int[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15 };
// filter to even
Func<int, boolwhere = x =x % 2 == 0;
// or divisible by 3
where = where.Or(x =x % 3 == 0);
// and greater than 5
where = where.And(x =x 5);
// get the results
int[] results = data.Where(where).ToArray();
}
Jan 28 '08 #6
Thank you!

Yes I was struggling with the Or. No I have to figure out how to incorporate
the values of a selected checkboxes inside the ListBox list into the where
clause.
But I still have to get the picture, the linq syntax using predicates is
still somewhat confusing for me.

That's why I liked Scotts Gu's blog because the dynamic string syntax is
very comfortable to work with. But I guess this is not the way-to-linq.
Evert

"Marc Gravell" wrote:
(generic Or/And)

static Func<T, boolOr<T>(this Func<T, boollhs, Func<T, bool>
rhs) {
return x =lhs(x) || rhs(x);
}
static Func<T, boolAnd<T>(this Func<T, boollhs, Func<T, bool>
rhs) {
return x =lhs(x) && rhs(x);
}
Jan 28 '08 #7
That's why I liked Scotts Gu's blog because the dynamic string syntax is
very comfortable to work with. But I guess this is not the way-to-linq.
The string approach is useful as a fallback, but if you know the types
I would use compiled code. For completeness, here is the Expression
equivalents for combining and/or:

static Expression<Func<T, bool>And<T>(this Expression<Func<T,
bool>lhs, Expression<Func<T, bool>rhs) {
ParameterExpression param = Expression.Parameter(typeof(T),
"x");
return Expression.Lambda<Func<T,
bool>>(Expression.AndAlso(Expression.Invoke(lhs, param),
Expression.Invoke(rhs, param)), param);
}
static Expression<Func<T, bool>Or<T>(this Expression<Func<T,
bool>lhs, Expression<Func<T, bool>rhs) {
ParameterExpression param = Expression.Parameter(typeof(T),
"x");
return Expression.Lambda<Func<T,
bool>>(Expression.OrElse(Expression.Invoke(lhs, param),
Expression.Invoke(rhs, param)), param);
}
Jan 28 '08 #8
Thank you for your help!

I will dive further into your examples and consider (in advance) it answered.

Evert

"Marc Gravell" wrote:
That's why I liked Scotts Gu's blog because the dynamic string syntax is
very comfortable to work with. But I guess this is not the way-to-linq.

The string approach is useful as a fallback, but if you know the types
I would use compiled code. For completeness, here is the Expression
equivalents for combining and/or:

static Expression<Func<T, bool>And<T>(this Expression<Func<T,
bool>lhs, Expression<Func<T, bool>rhs) {
ParameterExpression param = Expression.Parameter(typeof(T),
"x");
return Expression.Lambda<Func<T,
bool>>(Expression.AndAlso(Expression.Invoke(lhs, param),
Expression.Invoke(rhs, param)), param);
}
static Expression<Func<T, bool>Or<T>(this Expression<Func<T,
bool>lhs, Expression<Func<T, bool>rhs) {
ParameterExpression param = Expression.Parameter(typeof(T),
"x");
return Expression.Lambda<Func<T,
bool>>(Expression.OrElse(Expression.Invoke(lhs, param),
Expression.Invoke(rhs, param)), param);
}
Jan 29 '08 #9
Maybe the following helps other people:

For my problem I found (another) solution:

In a foreach loop I put all the values of checked items inside my
CheckedListBoxControl in a generic List of type string. Then I (equi) join
this list with my query.

"Evert" wrote:
Thank you for your help!

I will dive further into your examples and consider (in advance) it answered.

Evert

"Marc Gravell" wrote:
That's why I liked Scotts Gu's blog because the dynamic string syntax is
very comfortable to work with. But I guess this is not the way-to-linq.
The string approach is useful as a fallback, but if you know the types
I would use compiled code. For completeness, here is the Expression
equivalents for combining and/or:

static Expression<Func<T, bool>And<T>(this Expression<Func<T,
bool>lhs, Expression<Func<T, bool>rhs) {
ParameterExpression param = Expression.Parameter(typeof(T),
"x");
return Expression.Lambda<Func<T,
bool>>(Expression.AndAlso(Expression.Invoke(lhs, param),
Expression.Invoke(rhs, param)), param);
}
static Expression<Func<T, bool>Or<T>(this Expression<Func<T,
bool>lhs, Expression<Func<T, bool>rhs) {
ParameterExpression param = Expression.Parameter(typeof(T),
"x");
return Expression.Lambda<Func<T,
bool>>(Expression.OrElse(Expression.Invoke(lhs, param),
Expression.Invoke(rhs, param)), param);
}
Feb 4 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ronald S. Cook | last post by:
I've always been taught that stored procedures are better than writing SQL in client code for a number of reasons: - runs faster as is compiled and lives on the database server - is the more...
15
by: EDBrian | last post by:
My problem is this. Our clients create different fields they want to collect and we allow them build dynamic filters, reports etc... We run some TSQL to actually create the column and all works...
1
by: Lacutas | last post by:
Hi I'm having some problems getting a dynamic LINQ query to work on my DataSet. The idea is that a user selects certain criteria, and then the LINQ query filters through the dataset making the...
1
by: john | last post by:
I'm trying to build a LINQ expression that will use a dynamic construction of a LIKE statement in the WHERE clause, it would look something like this in SQL: WHERE TaskGroup Like "*00*" OR...
1
by: Frederik | last post by:
Hi all, Am I correct when stating that LINQ replaces somewhat DataTables? I have done some reading concerning LINQ, but I'm still puzzled as to whether I should use LINQ or not. My application...
10
by: Jonathan | last post by:
Hi all, I have a file consisting fixed width records from which I need to extract only those lines meeting certain conditions. These conditions do change and I find myself recoding/compiling...
0
by: Satish | last post by:
Hi everyone, I need some help constructing a linq query on datatables. I have 2 datatables as follows RangeTable MinValue MaxValue 0.00 0.050 0.051 0.10 0.101 0.15 0.151 0.20
4
by: acgritt | last post by:
I am looking for some information on if it is possible/how to create a dynamic XLinq statement. I know for Linq to SQL you can do the following: var query = db.Customers. Where("City = @0 and...
3
by: Vivien Parlat | last post by:
Hello, I am currently using VB.Net 2008 express. I use linq to perform queries on a database, and I'm using the following link's source to convert those queries into DataTables i can then bind...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.