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(the StonglyTypedRow =theStonglyType dRow .SomeColumn
== checkedListBox. Item[index] .Value)
}
}
But then I was caught by 'Deffered Execution' (my old construction was based
on DataView.RowFil ter 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\D ynamicQuery'. 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 9 14046
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(the StonglyTypedRow =theStonglyType dRow .SomeColumn
== checkedListBox. Item[index] .Value)
}
}
But then I was caught by 'Deffered Execution' (my old construction was based
on DataView.RowFil ter 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\D ynamicQuery'. 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
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
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
(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);
}
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(wher e).ToArray();
}
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);
}
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>(thi s Expression<Func <T,
bool>lhs, Expression<Func <T, bool>rhs) {
ParameterExpres sion param = Expression.Para meter(typeof(T) ,
"x");
return Expression.Lamb da<Func<T,
bool>>(Expressi on.AndAlso(Expr ession.Invoke(l hs, param),
Expression.Invo ke(rhs, param)), param);
}
static Expression<Func <T, bool>Or<T>(this Expression<Func <T,
bool>lhs, Expression<Func <T, bool>rhs) {
ParameterExpres sion param = Expression.Para meter(typeof(T) ,
"x");
return Expression.Lamb da<Func<T,
bool>>(Expressi on.OrElse(Expre ssion.Invoke(lh s, param),
Expression.Invo ke(rhs, param)), param);
}
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>(thi s Expression<Func <T,
bool>lhs, Expression<Func <T, bool>rhs) {
ParameterExpres sion param = Expression.Para meter(typeof(T) ,
"x");
return Expression.Lamb da<Func<T,
bool>>(Expressi on.AndAlso(Expr ession.Invoke(l hs, param),
Expression.Invo ke(rhs, param)), param);
}
static Expression<Func <T, bool>Or<T>(this Expression<Func <T,
bool>lhs, Expression<Func <T, bool>rhs) {
ParameterExpres sion param = Expression.Para meter(typeof(T) ,
"x");
return Expression.Lamb da<Func<T,
bool>>(Expressi on.OrElse(Expre ssion.Invoke(lh s, param),
Expression.Invo ke(rhs, param)), param);
}
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
CheckedListBoxC ontrol 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>(thi s Expression<Func <T,
bool>lhs, Expression<Func <T, bool>rhs) {
ParameterExpres sion param = Expression.Para meter(typeof(T) ,
"x");
return Expression.Lamb da<Func<T,
bool>>(Expressi on.AndAlso(Expr ession.Invoke(l hs, param),
Expression.Invo ke(rhs, param)), param);
}
static Expression<Func <T, bool>Or<T>(this Expression<Func <T,
bool>lhs, Expression<Func <T, bool>rhs) {
ParameterExpres sion param = Expression.Para meter(typeof(T) ,
"x");
return Expression.Lamb da<Func<T,
bool>>(Expressi on.OrElse(Expre ssion.Invoke(lh s, param),
Expression.Invo ke(rhs, param)), param);
}
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 proper tier to put it since is a data function
But then I've heard that writing SQL in my client .NET code might run just
as fast? Dynamic SQL or something?
And then there's LINQ on the horizon. Is it a successor to everything
|
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 very well. We
are now adding a lot more functionality to our filters and could really
benefit from using the LINQ to SQL.
I have experimented with the Dynamic Linq...
|
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 requested changes.
The table I am trying to alter is called RawData, this is defined in a DataSet but has no data columns as these aren't known until some data is imported. As part of my import routine it automatically creates the correct columns...
|
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 TaskGroup Like "*20*"
It would be many variations on the above.
|
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 imports data from an
XML file and stores the data in a DataSet (with 3 DataTables in it).
The DataTables are used to fill several DataGridViews (via DataViews/
BindingSources). When the user saves his changes, the DataTables are
used to rebuild...
| |
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 for each
set of conditions then running again.
e.g.
|
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
|
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 Orders.Count >= @1", "London", 10).
OrderBy("CompanyName").
Select("new(CompanyName as Name, Phone)");
I am wondering how to do this with Linq to XML. I know I can do the
|
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 to WinForms'
DataGridViews:
http://blogs.msdn.com/aconrad/archive/2007/09/07/science-project.aspx
The point is, the properties names extracted by the following lines:
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |