List Joining Ordering And Filtering With Linq : List « Data Structure « C# / CSharp Tutorial






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


    class ProductWithSupplierID
    {
        public string Name { get; private set; }
        public decimal Price { get; private set; }
        public int SupplierID { get; private set; }

        public ProductWithSupplierID(string name, decimal price)
        {
            Name = name;
            Price = price;
        }

        ProductWithSupplierID()
        {
        }

        public static List<ProductWithSupplierID> GetSampleProducts()
        {
            return new List<ProductWithSupplierID>
            {
                new ProductWithSupplierID { Name="C", Price = 9.99m, SupplierID=1 },
                new ProductWithSupplierID { Name="A", Price=14.99m, SupplierID=2 },
                new ProductWithSupplierID { Name="F", Price=13.99m, SupplierID=1 },
                new ProductWithSupplierID { Name="S", Price=10.99m, SupplierID=3}
            };
        }

        public override string ToString()
        {
            return string.Format("{0}: {1}", Name, Price);
        }
    }
    class Supplier
    {
        public string Name { get; private set; }
        public int SupplierID { get; private set; }

        Supplier()
        {
        }

        public static List<Supplier> GetSampleSuppliers()
        {
            return new List<Supplier>
            {
                new Supplier { Name="S", SupplierID=1 },
                new Supplier { Name="C", SupplierID=2 },
                new Supplier { Name="B", SupplierID=3 }
            };
        }
    }    
    class ListJoiningOrderingAndFilteringWithLinq
    {
        static void Main()
        {
            List<ProductWithSupplierID> products = ProductWithSupplierID.GetSampleProducts();
            List<Supplier> suppliers = Supplier.GetSampleSuppliers();
            var filtered = from p in products
                           join s in suppliers
                           on p.SupplierID equals s.SupplierID
                           where p.Price > 10
                           orderby s.Name, p.Name
                           select new
                           {
                               SupplierName = s.Name,
                               ProductName = p.Name
                           };
            foreach (var v in filtered)
            {
                Console.WriteLine("Supplier={0}; Product={1}",
                                  v.SupplierName, v.ProductName);
            }
        }
    }








11.34.List
11.34.1.Obtaining a read-only copy of a list
11.34.2.Using the Action delegate
11.34.3.Converting a list from list of string to list of int
11.34.4.Converting a list: user defined converting function
11.34.5.Vector extends List
11.34.6.List Filtering With Linq
11.34.7.List Joining Ordering And Filtering With Linq
11.34.8.List Order With Extension Method
11.34.9.List Query With Delegates
11.34.10.List Query With Delegates Compact
11.34.11.List Query With Lambda Expression
11.34.12.List Sort With Comparer
11.34.13.List Sort With Comparison Delegate
11.34.14.Implement IComparable to use List.Sort
11.34.15.List.ForEach
11.34.16.Compact Code for looping through the List with delegate
11.34.17.List Convert All
11.34.18.Using external method to pass into Find method
11.34.19.List size and capacity
11.34.20.List range operation
11.34.21.Use Action<(Of <(T>)>) delegate to print the contents of a List<(Of <(T>)>) object.
11.34.22.Item property (the indexer in C#) and various other properties and methods of the List<(Of <(T>)>) generic class.