Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. linq to sql: conditional where clause

linq to sql: conditional where clause

Scheduled Pinned Locked Moved LINQ
databasehelpcsharplinqquestion
7 Posts 4 Posters 18 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ezazazel
    wrote on last edited by
    #1

    I have a problem getting linqtosql working right. I have three strings, where each one can be string.empty. In the query I have three where clauses, each one checks one cell for if the value (string) starts with the text given. The problem is, that if the string is empty, the result is always empty, because none of the values starts with string.empty. What I need is a statement which allows me to add conditional where clauses - e.g if the string is empty skip the where clause, else use it. For better understanding: string s = textbox1.Text; string t = textbox2.Text; string u = textbox3.Text; var query = (select p from database where p.questionablestring1.StartsWith(s) //don' t do it if s == string.Empty where p.questionablestring2.StartsWith(t)//don' t do it if t == string.Empty where p.questionablestring3.StartsWith(u)//don' t do it if u == string.Empty select p).ToList(); Neither this works where p.questionablestring1.StartsWith(s != string.Empty ? s : null) //Given Error: must not be null nor this var query = (select p from database (if (s != string.Empty) {p.questionablestring1.StartsWith(s) }) select p).ToList(); Thanks in advance for your help.

    P D 2 Replies Last reply
    0
    • E ezazazel

      I have a problem getting linqtosql working right. I have three strings, where each one can be string.empty. In the query I have three where clauses, each one checks one cell for if the value (string) starts with the text given. The problem is, that if the string is empty, the result is always empty, because none of the values starts with string.empty. What I need is a statement which allows me to add conditional where clauses - e.g if the string is empty skip the where clause, else use it. For better understanding: string s = textbox1.Text; string t = textbox2.Text; string u = textbox3.Text; var query = (select p from database where p.questionablestring1.StartsWith(s) //don' t do it if s == string.Empty where p.questionablestring2.StartsWith(t)//don' t do it if t == string.Empty where p.questionablestring3.StartsWith(u)//don' t do it if u == string.Empty select p).ToList(); Neither this works where p.questionablestring1.StartsWith(s != string.Empty ? s : null) //Given Error: must not be null nor this var query = (select p from database (if (s != string.Empty) {p.questionablestring1.StartsWith(s) }) select p).ToList(); Thanks in advance for your help.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      The following pops into mind (using boolean shortcutting):

      string s = textbox1.Text;
      string t = textbox2.Text;
      string u = textbox3.Text;

      var query = (select p from database
      where (!string.IsNullOrempty(s) && p.questionablestring1.StartsWith(s))
      || (!string.IsNullOrempty(t) && p.questionablestring2.StartsWith(t))
      || (!string.IsNullOrempty(u) && p.questionablestring3.StartsWith(u))
      select p).ToList();

      I've just knocked this together quickly, but the principle seems as though it should be sound.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      E 1 Reply Last reply
      0
      • E ezazazel

        I have a problem getting linqtosql working right. I have three strings, where each one can be string.empty. In the query I have three where clauses, each one checks one cell for if the value (string) starts with the text given. The problem is, that if the string is empty, the result is always empty, because none of the values starts with string.empty. What I need is a statement which allows me to add conditional where clauses - e.g if the string is empty skip the where clause, else use it. For better understanding: string s = textbox1.Text; string t = textbox2.Text; string u = textbox3.Text; var query = (select p from database where p.questionablestring1.StartsWith(s) //don' t do it if s == string.Empty where p.questionablestring2.StartsWith(t)//don' t do it if t == string.Empty where p.questionablestring3.StartsWith(u)//don' t do it if u == string.Empty select p).ToList(); Neither this works where p.questionablestring1.StartsWith(s != string.Empty ? s : null) //Given Error: must not be null nor this var query = (select p from database (if (s != string.Empty) {p.questionablestring1.StartsWith(s) }) select p).ToList(); Thanks in advance for your help.

        D Offline
        D Offline
        Daniel Grunwald
        wrote on last edited by
        #3

        You can build a query step-by-step: var query = database; if (s != string.Empty) query = query.Where(p => p.questionablestring1.StartsWith(s)) if (t != string.Empty) query = query.Where(p => p.questionablestring2.StartsWith(t)) // you can also use LINQ syntax here: if (u != string.Empty) query = from p in query where p.questionablestring3.StartsWith(u) select p; var result = query.ToList();

        E M 2 Replies Last reply
        0
        • P Pete OHanlon

          The following pops into mind (using boolean shortcutting):

          string s = textbox1.Text;
          string t = textbox2.Text;
          string u = textbox3.Text;

          var query = (select p from database
          where (!string.IsNullOrempty(s) && p.questionablestring1.StartsWith(s))
          || (!string.IsNullOrempty(t) && p.questionablestring2.StartsWith(t))
          || (!string.IsNullOrempty(u) && p.questionablestring3.StartsWith(u))
          select p).ToList();

          I've just knocked this together quickly, but the principle seems as though it should be sound.

          Deja View - the feeling that you've seen this post before.

          My blog | My articles

          E Offline
          E Offline
          ezazazel
          wrote on last edited by
          #4

          that's what I've been looking for. Thanx a lot and sorry for the late reply

          P 1 Reply Last reply
          0
          • D Daniel Grunwald

            You can build a query step-by-step: var query = database; if (s != string.Empty) query = query.Where(p => p.questionablestring1.StartsWith(s)) if (t != string.Empty) query = query.Where(p => p.questionablestring2.StartsWith(t)) // you can also use LINQ syntax here: if (u != string.Empty) query = from p in query where p.questionablestring3.StartsWith(u) select p; var result = query.ToList();

            E Offline
            E Offline
            ezazazel
            wrote on last edited by
            #5

            not bad as well - very helpful for understanding the possibilities of linq. Thank you for your tip.

            1 Reply Last reply
            0
            • E ezazazel

              that's what I've been looking for. Thanx a lot and sorry for the late reply

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              ezazazel wrote:

              Forum:LINQ and .NET 3.5 Subject:Re: linq to sql: conditional where clause Sender:ezazazel Date:Sunday, March 30, 2008 9:44 PM that's what I've been looking for. Thanx a lot and sorry for the late reply

              Hey, no problem. I figured that it had worked because you hadn't been back asking for follow up.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              1 Reply Last reply
              0
              • D Daniel Grunwald

                You can build a query step-by-step: var query = database; if (s != string.Empty) query = query.Where(p => p.questionablestring1.StartsWith(s)) if (t != string.Empty) query = query.Where(p => p.questionablestring2.StartsWith(t)) // you can also use LINQ syntax here: if (u != string.Empty) query = from p in query where p.questionablestring3.StartsWith(u) select p; var result = query.ToList();

                M Offline
                M Offline
                murali_utr
                wrote on last edited by
                #7

                hi Daniel, this one is AND condition. How to implement OR Condition. Thanks in Advance!

                Have A Nice Day! Murali.M
                Blog

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups