Select Distinct : select « LINQ « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

    class Customer
    {
        public string ID { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
        public string Region { get; set; }
        public decimal Sales { get; set; }

        public override string ToString()
        {
            return "ID: " + ID + " City: " + City + " Country: " + Country + " Region: " + Region + " Sales: " + Sales;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Customer> customers = new List<Customer> {
              new Customer { ID="Q", City="London", Country="UK", Region="Europe", Sales=8000 },
              new Customer { ID="R", City="Beijing", Country="China", Region="Asia", Sales=9000 },
              new Customer { ID="T", City="Lima", Country="Peru", Region="South America", Sales=2002 }
           };

            var queryResults = customers.Select(c => c.Region).Distinct();
            foreach (var item in queryResults)
            {
                Console.WriteLine(item);
            }
        }
    }








22.63.select
22.63.1.Query Reuse
22.63.2.Select array item by type
22.63.3.Use LINQ with Dictionary
22.63.4.Use LINQ to query characters in a string
22.63.5.Transformation: link two array.
22.63.6.Select with Anonymous Types: prints uppercase and lowercase versions of each string in an input array
22.63.7.Select with Anonymous Types: iterates over each element to print the element's name
22.63.8.Select with Anonymous Types: prints the name of every product in the product list along with the category of the product and its unit price
22.63.9.Using select to create a sequence of each product name.
22.63.10.Converting an Array of Strings to Integers
22.63.11.Converting an Array of Strings to Integers and Sorting It
22.63.12.Query with Intentional Exception Deferred Until Enumeration
22.63.13.Demonstrating the Query Results Changing Between Enumerations
22.63.14.Returning a List
22.63.15.Select Distinct
22.63.16.Select all from a List
22.63.17.Select only pet name
22.63.18.Select sum value
22.63.19.Delegate targets
22.63.20.Adding new line character to selected value