Query customers live in Colorado - CSharp LINQ

CSharp examples for LINQ:where

Description

Query customers live in Colorado

Demo Code



using System;/* w w w . j a va 2  s  . c  o  m*/
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var customers = new List<Customer> {  // Get some customers.
        new Customer { Name = "Javascript", State = "OR", Phone = "555-555-5555" },
        new Customer { Name = "XML", State = "CO", Phone = "555-666-6666" },
        new Customer { Name = "Oracle", State = "CO", Phone = "555-777-7777" },
        new Customer { Name = "MySQL", State = "NY", Phone = "555-000-0000" },
        new Customer { Name = "MongoDB", State = "NY", Phone = "555-111-1111" },
        new Customer { Name = "Java", State = "NJ", Phone = "555-222-2222" }
      };
        var coloradoCustomers = from c in customers
                                where c.State == "CO"
                                select c;
        Console.WriteLine("\tCustomers in Colorado (CO):");
        foreach (var cust in coloradoCustomers)
        {
            Console.WriteLine("\t\t{0} - {1}", cust.Name, cust.State);
        }

    }
}
class Customer
{
    public string Name;
    public string State;
    public string Phone;
}

Result


Related Tutorials