Do a projection to a new anonymous object for each customer. - CSharp LINQ

CSharp examples for LINQ:Projection

Description

Do a projection to a new anonymous object for each customer.

Demo Code



using System;//  w  w w  . j  av  a  2s  . 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 namesAndNumbers = from c in customers orderby c.Name

        select new { Name = c.Name, PhoneNumber = c.Phone };
   
        foreach (var cust in namesAndNumbers)
        {
            Console.WriteLine("\t{0} - {1}", cust.Name.PadRight(10), cust.PhoneNumber);
        }


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

Result


Related Tutorials