Using an "and" in LINQ

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • napstar
    New Member
    • Nov 2006
    • 55

    Using an "and" in LINQ

    Is it possible to use an "and" in LINQ i.e:

    var londonCustomers = from customer in Customers
    where customer.City == "London"

    and customer.Credit CardType="Visa"
    select customer;
    if yes/no how do I achieve this?
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    Code:
    var londonCustomers = from c in Customers
                          where (c.City.Equals("London") && c.CreditCardType.Equals("Visa"))
                          select c;
    If you only needed certain parts, then you'd use the select to filter the relevant fields. For example:

    Code:
    from c in Customers
    where customer.City.Equals("London")
    select new { Name = c.Name, PostCode = c.PostCode }
    You could also use method syntax instead of query syntax:

    Code:
    var londonCustomers = Customers.Where(c => c.City.Equals("London") && c.CreditCardType.Equals("Visa"));
    Of course, you'd need to understand lambda expressions to be able to do that.

    You could also do this without LINQ:

    Code:
    var londonCustomers.Find(c => c.City.Equals("London") && c.CreditCardType.Equals("Visa"));
    The beauty of the non-LINQ syntax is that it works in .NET 2.0 codebases

    When you're thinking about LINQ, it does have some similarities to SQL, but it's not the same. You need to think about it slightly differently. Sure you've got your "from c in Customers", but your "where" isn't structured the same way as in SQL:

    The where clause is basically the same as your .NET 2.0 find. Your query is iterating every object in your collection, so you treat each object exactly as you would if you were writing code relating to it - think of an if statement:

    Code:
    if (c.Location.Equals("London") && (c.PostCode.StartsWith("EC1") || c.PostCode.StartsWith("SW11")) { ... }
    Your where clause is exactly the same...
    Last edited by balabaster; Sep 3 '10, 01:04 PM.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Originally posted by balabaster
      Balabaster: You could also do this without LINQ:

      Code:
      var londonCustomers.Find(c => c.City.Equals("London") && c.CreditCardType.Equals("Visa"));
      The beauty of the non-LINQ syntax is that it works in .NET 2.0 codebases
      I'm a bit confused.
      The non-linq solution makes use of lambda expressions but I didn't think they were available until 3.5?
      Could you clarify what you mean?

      -Frinny

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        You know what, you may be right about the lambda expressions, they may not have been available until 3.5. To be honest, I'm not entirely sure. However, the lambda expressions will still work with the non-LINQ code in 3.5 for sure, it's possible that they won't work in 2.0.

        Comment

        Working...