How to filter with LINQ?
  Home FAQ Contact Sign in
microsoft.public.vsnet.general only
 
Advanced search
POPULAR GROUPS

more...

microsoft.public.vsnet.general Profile…
 Up
How to filter with LINQ?         


Author: Juan Dent
Date: May 11, 2008 23:17

Hi,

I have a bunch of textboxes which might contain a value to filter, but may
be also empty.
I have a LINQ like so:

----
var someAdministrators = from a in db.ADMINISTRATORs

where XXXXX

----
how can I incorporate the variable amount of where clauses? this is a 2 to
the number of clauses problem.

--
Thanks in advance,

Juan Dent, M.Sc.
3 Comments
RE: How to filter with LINQ?         


Author: Wen Yuan Wang [MSFT]
Date: May 12, 2008 03:24

Hello Juan,

I'm not sure I have understood your issue completely. It seems you have a
lot of textboxs which contains the value need to be used in Where case.
Something likes the following:

var someAdministrators = from a in db.ADMINISTRATORs
where (a.c1==textbox1.value) && (a.c2==textbox2.value) &&
(a.c3==textbox3.value)
select a;

Is this what you need? Please let me know if this is what you are after. If
I misunderstood anything, please also don't hesitate to correct me. We will
follow up. We are glad to assist you.

Have a great day,
Best regards,
Wen Yuan

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of...
Show full article (1.97Kb)
no comments
RE: How to filter with LINQ?         


Author: Juan Dent
Date: May 12, 2008 11:21

Hi,

No, the problem is that some of these textboxes may be empty and thus should
not be taken account in the where clause.

This makes it an exponential combination of where sub-clauses.

Is this clear?
--
Thanks in advance,

Juan Dent, M.Sc.

""Wen Yuan Wang [MSFT]"" wrote:
> Hello Juan,
>
> I'm not sure I have understood your issue completely. It seems you have a
> lot of textboxs which contains the value need to be used in Where case.
> Something likes the following...
Show full article (2.34Kb)
no comments
RE: How to filter with LINQ?         


Author: Wen Yuan Wang [MSFT]
Date: May 13, 2008 00:52

Hello Juan,

It seems like you want empty textbox NOT to be taken account in the where
clause, correct? If I misunderstood anything again, please don't hesitate
to correct me.

One way is to create Linq Query dynamically by Express Tree. But, the code
will be hard to maintain and read. My suggestion is that we can check the
textbox value in the conditional operator (?:). It returns one of two
values depending on the value of a Boolean expression. eg:

var someAdministrators = from a in db.ADMINISTRATORs
where
(textbox1.value==""?a.c1==textbox1.value:true)
&&
(textbox2.value==""?a.c2==textbox2.value:true)
&&
(textbox3.value==""?a.c3==textbox3.value:true)
select a;

Hope this helps. Please try the above method and let me know if this is
what you need. If there is anything unclear, please also feel free to let
us know. We are glad to assist you.
Show full article (1.61Kb)
no comments