Restriction Operators

[Back to LINQ examples] View Original LINQ Code

Where - Simple 1

This sample finds all elements of an array less than 5.

This is one of the LINQ examples designed to show LINQ operating over standard Java collections. In this case the batch version is just ordinary Java iteration.

private void Batch1() {
  print("Numbers < 5:");
  int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  for (int x : numbers) {
    if (x < 5)
      print("{0}", x);
  }
}

Original LINQ Code:

public void Linq1()
{
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

    var lowNums =
        from n in numbers
        where n < 5
        select n;

    Console.WriteLine("Numbers < 5:");
    foreach (var x in lowNums)
    {
        Console.WriteLine(x);
    }
}

Where - Simple 2

This sample uses finds all products that are out of stock.

private void Batch2() {
  for (Northwind db : connection) {
    print("Sold out products:");
    for (Product product : db.Products)
      if (product.UnitsInStock == 0)
        print("{0} is sold out!", product.ProductName);
  }
}

SQL Code generated:

SELECT T1.ProductName AS g37
FROM Products T1
WHERE (T1.UnitsInStock=0)

Original LINQ Code:

public void Linq2()
{
    List<Product> products = GetProductList();

    var soldOutProducts =
        from p in products
        where p.UnitsInStock == 0
        select p;

    Console.WriteLine("Sold out products:");
    foreach (var product in soldOutProducts)
    {
        Console.WriteLine("{0} is sold out!", product.ProductName);
    }
}

Where - Simple 3

This sample finds all products that are in stock and cost more than 3.00 per unit.

public void Batch3() {
  for (Northwind db : connection) {
    print("In-stock products that cost more than 3.00:");
    for (Product p : db.Products)
      if (p.UnitsInStock > 0 && p.UnitPrice > 3.00)
        print("{0} is in stock and costs more than 3.00.", p.ProductName);
  }
}

Generated SQL Code:

SELECT T1.ProductName AS g38
FROM Products T1
WHERE ((T1.UnitsInStock>0) AND (T1.UnitPrice>3.0))

Original LINQ Code:

public void Linq3()
{
    List<Product> products = GetProductList();

    var expensiveInStockProducts =
        from p in products
        where p.UnitsInStock > 0 && p.UnitPrice > 3.00M
        select p;

    Console.WriteLine("In-stock products that cost more than 3.00:");
    foreach (var product in expensiveInStockProducts)
    {
        Console.WriteLine("{0} is in stock and costs more than 3.00.", product.ProductName);
    }
}

Where - Drilldown

This sample finds all customers in Washington and then uses the resulting sequence to drill down into their orders.

public void Batch4() {
  print("Customers from Washington and their orders:");
  for (Northwind db : connection)
    for (Customer c : db.Customers)
      if (c.Region == "WA") {
        print("Customer {0}: {1}", c.CustomerID, c.CompanyName);
        for (Order order : c.Orders)
          print(" Order {0}: {1}", order.OrderID, order.OrderDate);
      }
}

Note that exactly two queries are used to load all the data. A batch always
generates a statically-determined constant number of queries, no matter how many loops or
data are loaded.

SELECT T1.CustomerID AS g39, T1.CompanyName AS g40, T1.CustomerID AS id
FROM Customers T1
WHERE (T1.Region="WA")
ORDER BY id ASC

SELECT T3.CustomerID AS parent, T3.OrderID AS g41,T3.OrderDate AS g42
FROM Orders T3 INNER JOIN Customers T4 ON (T4.CustomerID=T3.CustomerID)
WHERE (T4.Region="WA")
ORDER BY parent ASC

Original LINQ Code:

public void Linq4()
{
    List<Customer> customers = GetCustomerList();

    var waCustomers =
        from c in customers
        where c.Region == "WA"
        select c;

    Console.WriteLine("Customers from Washington and their orders:");
    foreach (var customer in waCustomers)
    {
        Console.WriteLine("Customer {0}: {1}", customer.CustomerID, customer.CompanyName);
        foreach (var order in customer.Orders)
        {
            Console.WriteLine("  Order {0}: {1}", order.OrderID, order.OrderDate);
        }
    }
}

Where - Indexed

This sample demonstrates an indexed Where clause that returns digits whose name is shorter than their value.

This is another sample that does works on in memory data. A normal for loop and a conditional work just fine.

private void Batch5() {
  String[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

  print("Short digits:");
  int index = 0;
  for (String d : digits) {
    if (d.length() < index)
      print("The word {0} is shorter than its value.", d);
    index++;    
  }
}

Original LINQ Code:

public void Linq5()
{
    string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

    var shortDigits = digits.Where((digit, index) => digit.Length < index);

    Console.WriteLine("Short digits:");
    foreach (var d in shortDigits)
    {
        Console.WriteLine("The word {0} is shorter than its value.", d);
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License