• Linq知识大全


    select的源码public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);

     class Program
    {
    static void Main(string[] args)
    {
        var customers = new[] {  
            new { CustomerID = 1, FirstName = "Orlando", LastName = "Gee",  
                    CompanyName = "A Bike Store" },
            new { CustomerID = 2, FirstName = "Keith", LastName = "Harris",  
                    CompanyName = "Bike World" },
            new { CustomerID = 3, FirstName = "Donna", LastName = "Carreras",  
                    CompanyName = "A Bike Store" },
            new { CustomerID = 4, FirstName = "Janet", LastName = "Gates",  
                        CompanyName = "Fitness Hotel" },
            new { CustomerID = 5, FirstName = "Lucy", LastName = "Harrington",  
                    CompanyName = "Grand Industries" },
            new { CustomerID = 6, FirstName = "David", LastName = "Liu",  
                    CompanyName = "Bike World" },
            new { CustomerID = 7, FirstName = "Donald", LastName = "Blanton",  
                    CompanyName = "Grand Industries" },
            new { CustomerID = 8, FirstName = "Jackie", LastName = "Blackwell",  
                    CompanyName = "Fitness Hotel" },
            new { CustomerID = 9, FirstName = "Elsa", LastName = "Leavitt",  
                    CompanyName = "Grand Industries" },
            new { CustomerID = 10, FirstName = "Eric", LastName = "Lang",  
                        CompanyName = "Distant Inn" }  
        };
        var addresses = new[] {  
            new { CompanyName = "A Bike Store", City = "New York", Country = "United States"},
            new { CompanyName = "Bike World", City = "Chicago", Country = "United States"},
            new { CompanyName = "Fitness Hotel", City = "Ottawa", Country = "Canada"},
            new { CompanyName = "Grand Industries", City = "London",Country = "United Kingdom"},
            new { CompanyName = "Distant Inn", City = "Tetbury", Country = "United Kingdom"}
        };

        //查询数据
        IEnumerable<string> customerFirstNames = customers.Select(cust => cust.FirstName);
        //IEnumerable<string> customerFirstNames = from custs in customers select custs.FirstName;
        foreach (string name in customerFirstNames)
        {
            Console.WriteLine(name);
        }
        Console.WriteLine(" ");

        IEnumerable<string> customerFullNames = customers.Select(cust => cust.FirstName + "  " + cust.LastName);
        //IEnumerable<string> customerFullNames = from custs in customers select custs.FirstName + "  " + custs.LastName;
        foreach (string name in customerFullNames)
        {
            Console.WriteLine(name);
        }

        Console.WriteLine(" ");
        var customerNames = customers.Select(cust => new { FirstName = cust.FirstName, LastName = cust.LastName });
        //var customerNames = from custs in customers select new { FirstName = custs.FirstName, LastName = custs.LastName };
        foreach (var name in customerNames)
        {
            Console.WriteLine(name);
        }

        //数据筛选
        Console.WriteLine(" ");
        IEnumerable<string> usCompanies = addresses.Where(addr => String.Equals(addr.Country, "United States"))
                                                    .Select(usComp => usComp.CompanyName);
        //IEnumerable<string> usCompanies = from adr in addresses
        //                                  where String.Equals(adr.Country, "United States")
        //                                  select adr.CompanyName;
        foreach (string name in usCompanies)
        {
            Console.WriteLine(name);
        }

        //数据排序
        Console.WriteLine(" ");
        IEnumerable<string> companyNames = addresses.OrderBy(addr => addr.CompanyName).Select(comp => comp.CompanyName);
        //IEnumerable<string> companyNames = from adr in addresses orderby adr.CompanyName select adr.CompanyName;
        foreach (string name in companyNames)
        {
            Console.WriteLine(name);
        }

        Console.WriteLine(" ");
        //数据分组---分组之后变成二维数组
        var companiesGroupedByCountry = addresses.GroupBy(addrs => addrs.Country);
        //var companiesGroupedByCountry = from adr in addresses group adr by adr.Country;
        foreach (var companiesPerCountry in companiesGroupedByCountry)
        {
            Console.WriteLine("Country: {0} {1} companies",
                    companiesPerCountry.Key, companiesPerCountry.Count());
            foreach (var companies in companiesPerCountry)
            {
                Console.WriteLine(" {0}", companies.CompanyName);
            }
        }

        //数据统计
        Console.WriteLine(" ");
        int numberOfCountries = addresses.Select(addr => addr.Country).Distinct().Count();
        //int numberOfCountries = (from adr in addresses select adr.Country).Distinct().Count();
        Console.WriteLine("Number of countries: {0}", numberOfCountries);

        //联合查询--内联查询
        Console.WriteLine(" ");
        var citiesAndCustomers = customers.Select(c =>
            new { c.FirstName, c.LastName, c.CompanyName })
            .Join(addresses, custs => custs.CompanyName, addrs => addrs.CompanyName, (custs, addrs) => new { custs.FirstName, custs.LastName, addrs.Country });

        //var citiesAndCustomers = from cuts in customers
        //                         join adr in addresses
        //                             on cuts.CompanyName equals adr.CompanyName
        //                         select new { cuts.FirstName, cuts.LastName, adr.Country };
        foreach (var row in citiesAndCustomers)
        {
            Console.WriteLine(row);
        }

        Console.ReadKey();
        }
    }
    }

     private static void Orderby()
            {
                int[] intAry1 = { 4, 3, 556, 34, 6, 2, 67 };
                var query1 = from var1 in intAry1
                             orderby var1 ascending
                             select var1;
                query1.ToList().ForEach(p => Console.Write(p + Environment.NewLine));
                var query2 = from var1 in intAry1
                             orderby var1 descending
                             select var1;
                query2.ToList().ForEach(p => Console.Write(p + Environment.NewLine));
                Console.WriteLine();
            }

            private static void Join左外部连接()
            {
                int[] intAry1 = { 5, 15, 25, 30, 33, 40 };
                int[] intAry2 = { 5, 20, 30, 30, 50, 70, 80 };
                var query1 = from var1 in intAry1
                             join var2 in intAry2 on var1 equals var2 into var2Grp //左连接 右连接相反
                             from grp in var2Grp.DefaultIfEmpty()
                             select new { a = var1 != 0 ? var1 : 100, b = grp };
                query1.ToList().ForEach(p => Console.Write("{0}:{1}", p.a, p.b + Environment.NewLine));
                Console.WriteLine();
            }

            private static void Join分组连接()
            {
                int[] intAry1 = { 5, 15, 25, 30, 33, 40 };
                int[] intAry2 = { 5, 20, 30, 30, 50, 70, 80 };
                var query1 = from var1 in intAry1
                             join var2 in intAry2 on var1 equals var2 into var2Grp
                             select new { a = var1, b = var2Grp };
                foreach (var item in query1)
                {
                    Console.Write("{0}:", item.a);
                    foreach (var o in item.b)
                    {
                        Console.Write("{0} ", o);
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
            }

            private static void Join内部连接()
            {
                int[] intAry1 = { 5, 15, 25, 30, 33, 40 };
                int[] intAry2 = { 5, 20, 30, 30, 50, 70, 80 };
                var query1 = from var1 in intAry1
                             join var2 in intAry2 on var1 equals var2
                             select new { a = var1, b = var2 };
                query1.ToList().ForEach(p => Console.Write(p.a + Environment.NewLine));
                Console.WriteLine();
            }

  • 相关阅读:
    ubuntu查看安装的pytorch/cuda版本
    go不使用工具包将大写字符转成小写字符的方法
    使用Nexus搭建Maven私服
    maven setting.xml配置说明
    maven的仓库、生命周期与插件
    maven项目搭建
    maven之详解继承与聚合
    Maven核心概念之依赖,聚合与继承
    commons-logging日志系统
    新建我的 第一个maven项目
  • 原文地址:https://www.cnblogs.com/shenqiboy/p/3148495.html
Copyright © 2020-2023  润新知