By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,185 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,185 IT Pros & Developers. It's quick & easy.

LINQ: How to build dynamic query?

P: n/a
Hello,
played a little with orcas and went into a problem by building dynamic
queries. I want to build a dynamic where clause but only with parameters that
are not empty =(TextBox.Text != "").
Doing this by building a string is very simple, but how to do it with LINQ?

string sql = "";
if (TextBox1.Text != "")
sql += "Columns_1='"+TextBox1.Text+"'";
if (TextBox2.Text != "")
sql += "Columns_2='"+TextBox2.Text+"'";
How to include the if query to linq???

var query = from c in db.MyTable
where c.Column_1 == TextBox1.Text &&
c.Column_2 == TextBox2.Text
select c;

Thank you!

-Stefan
Nov 14 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
(slightly simpler):

if (!string.IsNullOrEmpty(inputA)) {
query = query.Where(x =x.PropertyB == inputA);
}
if (!string.IsNullOrEmpty(inputB)) {
query = query.Where(x =x.PropertyB == inputB);
}

Marc
Nov 14 '07 #2

P: n/a
[watch for typo - should have been PropertyA]
Nov 14 '07 #3

P: n/a
It's actually slightly easier than that if you ignore the query
expression syntax:
...
Query expression syntax is nice in many cases, but just to add a
"where" clause I prefer to use the direct method call.
Yes - I posted that right after my initial post (albeit with a
typo)... I agree.
You could even write a method to abstract it out if necessary.
Care to expand on this? I can think of a few ways of interpreting
this, but I'm just interested in how you mean it...?

Marc

Nov 14 '07 #4

P: n/a
On Nov 14, 10:19 pm, Marc Gravell <marc.grav...@gmail.comwrote:
You could even write a method to abstract it out if necessary.

Care to expand on this? I can think of a few ways of interpreting
this, but I'm just interested in how you mean it...?
Well, you could have (off the top of my head - apologies if
Predicate<Tshould be Func<T,bool>)
(Further apologies for the formatting - Google groups uses a
proportional font)

IQueryable<TConditionalWhere<T>(static this IQueryable<Tquery,
string value,
Expression<Predicate<T>predicate)
{
if (!string.IsNullOrEmpty(value))
{
return query.Where(predicate);
}
}

then call it with:

var query = query.ConditionalWhere(valueA, x =x.PropertyA==valueA)
.ConditionalWhere(valueB, x =x.PropertyB==valueB);

Jon
Nov 15 '07 #5

P: n/a
You could presumably put an expression tree and lambda together
through code (using Expression.Property(...) at some point), but it
wouldn't be pretty. The switch would be easier to maintain.

Marc
Nov 15 '07 #6

P: n/a
Maybe it will work over reflection?
See my other reply
but in Windows.Forms this should
be not a problem.
winform is easy to abuse too... at least with ASP.NET the user doesn't
tend to have a direct connection to the database...

Marc
Nov 15 '07 #7

P: n/a
You could presumably put an expression tree and lambda together through
code (using Expression.Property(...) at some point), but it wouldn't be
pretty. The switch would be easier to maintain.
Some columns in my winform datagridview are defined by user in runtime.
So I cannot use hard-coded swith with all column names.

My grid columns can be string, bool and decimal types only.
Maybe it is possible to use switch for types only like

swith (SortExpression.GetTypeCode) {
case TypeCode.String:
....

case TypeCode.Bool:
...

case TypeCode.Decimal:
....

default:
MessageBox.Show("Cannot sort by this column");
}

Andrus.
Nov 15 '07 #8

P: n/a
There is an Expression.Property method (IIRC) that pairs to memberof;
I'll see what I can find...
Nov 16 '07 #9

P: n/a
Another question; the bit that you need to comment isn't clear, since
you don't actually change "query" with the comparer - query is still
"db.Klients"; is there a bit missing that might contain the glitch? It
may be unrelated, but it will be easier to investigate if you post the
verbatim code that isn't working...

Marc
Nov 18 '07 #10

P: n/a
Would it be possible for you to post the /generated/ code? Are you
familiar with "Reflector"? If you compile the *working* code
(x=>x.Nimi) and load it into Reflector, we could have a look at what
it generated? I should then be able to compare this to what the
dynamic code uses...

Unfortunately without runnable code this isn't something I can do
directly... but it has piqued my interest...

Marc
Nov 19 '07 #11

P: n/a
Would it be possible for you to post the /generated/ code? Are you
familiar with "Reflector"?
If you compile the *working* code
(x=>x.Nimi) and load it into Reflector, we could have a look at what
it generated? I should then be able to compare this to what the
dynamic code uses...
Generally No. I desided to download reflector sometime.
However before downloading it asks my personal data.
So I hit Cancel and did'nt download it.
I can study reflector if you need.
Unfortunately without runnable code this isn't something I can do
directly... but it has piqued my interest...
I sent test case to you by e-mail.

Andrus.
Nov 20 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.