Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
LINQ Build a where clause
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Razwan Sarwar  
View profile  
 More options Aug 25 2009, 6:37 am
From: Razwan Sarwar <sarwar.raz...@googlemail.com>
Date: Tue, 25 Aug 2009 03:37:07 -0700 (PDT)
Local: Tues, Aug 25 2009 6:37 am
Subject: LINQ Build a where clause
Hi

I am fairly new to LINQ and now face a problem that i can not solve
alone, i need help. I have tried many things but not found a solution
yet.

I have a webform with 2 dropdown lists and a text box. I am letting
the user build a where statement based on what they choose.
For example dropdownlist1: (dealername, dealernumber, location),
dropdownlist2(<,>,<=,>=,==), and textbox is a free text field.

The where will be built as "DealerNumber <= 1001"

Now my code, will it be possible to change the where part to
incorporate my controls? I am using "q = q.Where(c => c.DealerNumber
== strDealerNumber);"
can this be changed to:

field = dropdownlist1; condition = dropdownlist2; filter = textbox;
q = q.where(field + " " + condition + " " +  filter);

CODE:

            using (PartsClassesDataContext db = new
PartsClassesDataContext())
            {
                List<spSelectFromMasterPartsResult> lst =
db.spSelectFromMasterParts  ().ToList();

                var q = from c in lst
                        select c;

                 q = q.Where(c => c.DealerNumber == strDealerNumber);

                return q.ToList();
            }

Many thanks i appreciate all your help

Raz


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Dargavel  
View profile  
 More options Aug 25 2009, 8:27 am
From: Matt Dargavel <m...@dargs.co.uk>
Date: Tue, 25 Aug 2009 05:27:49 -0700 (PDT)
Local: Tues, Aug 25 2009 8:27 am
Subject: Re: LINQ Build a where clause

Most of the IQueryable methods, such as Where(), take Expression
Trees.  The lambda expressions you write in code are actually compiled
to these.  You can also build these up at runtime and the Expression
class contains functions to help with this.

I'm not familiar with these methods, but here are a few pages on MSDN
you may find useful:

http://msdn.microsoft.com/en-us/library/bb882637.aspx (How to: Use
Expression Trees to Build Dynamic Queries)

http://msdn.microsoft.com/en-us/library/bb397951.aspx (Expression
Trees)

http://msdn.microsoft.com/en-us/library/system.linq.expressions.aspx
(System.Linq.Expressions)

   Regards,

           Matt.

On Aug 25, 11:37 am, Razwan Sarwar <sarwar.raz...@googlemail.com>
wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrus Moor  
View profile  
 More options Aug 25 2009, 8:31 am
From: "Andrus Moor" <kobrule...@hot.ee>
Date: Tue, 25 Aug 2009 15:31:39 +0300
Local: Tues, Aug 25 2009 8:31 am
Subject: Re: [dblinq] LINQ Build a where clause
Look into DynamicLinqTest.cs for samples:

        public void DL1_Products()
        {
            Northwind db = CreateDB();

            var q = db.Products.Where("SupplierID=1 And UnitsInStock>2")
                .OrderBy("ProductID");
            var list = q.ToList();
            Assert.IsTrue(list.Count > 0, "Expected results from dynamic
query");
        }

        [Test]
        public void DL2_ProductCount()
        {
            Northwind db = CreateDB();

            int numProducts = db.Products.Where("SupplierID=1").Count();
            Assert.IsTrue(numProducts > 0, "Expected results from dynamic
query");
        }

        [Test]
        public void DL3_ProductCount()
        {
            Northwind db = CreateDB();

            int numProducts = db.Products.Count();
            Assert.IsTrue(numProducts > 0, "Expected results from dynamic
query");
        }

Andrus.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Dargavel  
View profile  
 More options Aug 25 2009, 11:00 am
From: Matt Dargavel <m...@dargs.co.uk>
Date: Tue, 25 Aug 2009 08:00:37 -0700 (PDT)
Local: Tues, Aug 25 2009 11:00 am
Subject: Re: LINQ Build a where clause

Ah, that makes it a bit easier! :-)

On Aug 25, 1:31 pm, "Andrus Moor" <kobrule...@hot.ee> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »