E
Evert
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
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