CSharp examples for LINQ:Select
Query all customers, but just their names and numbers using projection to an anonymous type
using System;/*w ww. j a v a 2 s.co 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 customerContacts = customers.OrderBy(c => c.Name).Select(c => new { Name = c.Name, PhoneNumber = c.Phone }); foreach (var c in customerContacts) { Console.WriteLine("\t\t{0} - {1}", c.Name.PadRight(10), c.PhoneNumber); } } } class Customer { public string Name; public string State; public string Phone; }