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. C#
  4. LINQ (C# 3.0)

LINQ (C# 3.0)

Scheduled Pinned Locked Moved C#
csharpquestionasp-netlinqsales
4 Posts 2 Posters 0 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.
  • A Offline
    A Offline
    Alvaro Mendez
    wrote on last edited by
    #1

    I'm trying to do the equivalent of this in LINQ:

    StringBuilder q = new StringBuilder("SELECT * FROM customer WHERE city = 'New York'");

    if (westCoast)
    q.Append(" OR city = 'Los Angeles'");

    Here's what I have so far:

    var q = dc.Customer.Where(c => c.City == "New York");

    if (westCoast)
    q.Where(c => c.City == "Los Angeles");

    But I end up with "WHERE city = 'New York' AND city = 'Los Angeles'"; How do I tell it to use OR instead? Thanks, Alvaro


    God existing isn't entirely impossible, but there's absolutely no evidence for it, so... the personal God as described by the Christian Bible existing is just as likely as a Pink Unicorn having created the universe, oh.. say... last Thursday. It's equally possible the moon has a core made of cheese. It's equally possible this sentence is in Spanish when you're not looking. - Someone on the Internet

    J 1 Reply Last reply
    0
    • A Alvaro Mendez

      I'm trying to do the equivalent of this in LINQ:

      StringBuilder q = new StringBuilder("SELECT * FROM customer WHERE city = 'New York'");

      if (westCoast)
      q.Append(" OR city = 'Los Angeles'");

      Here's what I have so far:

      var q = dc.Customer.Where(c => c.City == "New York");

      if (westCoast)
      q.Where(c => c.City == "Los Angeles");

      But I end up with "WHERE city = 'New York' AND city = 'Los Angeles'"; How do I tell it to use OR instead? Thanks, Alvaro


      God existing isn't entirely impossible, but there's absolutely no evidence for it, so... the personal God as described by the Christian Bible existing is just as likely as a Pink Unicorn having created the universe, oh.. say... last Thursday. It's equally possible the moon has a core made of cheese. It's equally possible this sentence is in Spanish when you're not looking. - Someone on the Internet

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      Hi Alvaro. I haven't used LINQ too much, but I understand the Where method takes a Predicate<Customer>. All you're doing is writing a lambda expression, kind of shorthand for an anonymous method. Here's a more verbose version that should work, assuming Where takes a predicate:

      var q = dc.Customer.Where(Criteria);

      ...

      bool Criteria(Customer input)
      {
      return input.City == "New York" || (westCoast && input.City == "Los Angeles");
      }

      I'm not sure, but you may be able to do this right inside the lambda:

      var q = dc.Customer.Where(c => c.City == "New York" || (westCoast && c.City == "Los Angeles"));

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

      A 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        Hi Alvaro. I haven't used LINQ too much, but I understand the Where method takes a Predicate<Customer>. All you're doing is writing a lambda expression, kind of shorthand for an anonymous method. Here's a more verbose version that should work, assuming Where takes a predicate:

        var q = dc.Customer.Where(Criteria);

        ...

        bool Criteria(Customer input)
        {
        return input.City == "New York" || (westCoast && input.City == "Los Angeles");
        }

        I'm not sure, but you may be able to do this right inside the lambda:

        var q = dc.Customer.Where(c => c.City == "New York" || (westCoast && c.City == "Los Angeles"));

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

        A Offline
        A Offline
        Alvaro Mendez
        wrote on last edited by
        #3

        Thanks for your reply Judah. I was more looking to do this in two steps, as an exercise. The first step sets up the base query, and then it would start growing based on different criteria. As I discovered, the default behavior is to AND the conditions together on multiple calls to Where. I was hoping someone would know how do change that to use OR instead. Regards, Alvaro


        God existing isn't entirely impossible, but there's absolutely no evidence for it, so... the personal God as described by the Christian Bible existing is just as likely as a Pink Unicorn having created the universe, oh.. say... last Thursday. It's equally possible the moon has a core made of cheese. It's equally possible this sentence is in Spanish when you're not looking. - Someone on the Internet

        J 1 Reply Last reply
        0
        • A Alvaro Mendez

          Thanks for your reply Judah. I was more looking to do this in two steps, as an exercise. The first step sets up the base query, and then it would start growing based on different criteria. As I discovered, the default behavior is to AND the conditions together on multiple calls to Where. I was hoping someone would know how do change that to use OR instead. Regards, Alvaro


          God existing isn't entirely impossible, but there's absolutely no evidence for it, so... the personal God as described by the Christian Bible existing is just as likely as a Pink Unicorn having created the universe, oh.. say... last Thursday. It's equally possible the moon has a core made of cheese. It's equally possible this sentence is in Spanish when you're not looking. - Someone on the Internet

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          I'm afraid I don't know how to do in 2 seperate pieces rather than 1 query with multiple conditions. Have you tried asking in the MSDN forums? I'm thinking too few people here have played with LINQ, so it might be tough getting an answer.

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

          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