Linq复杂对象查询

复杂的查询对象,

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

namespace LinqDemo
{
    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="A",City="New York",Country="USA",Region="North America",Sales=999 },
                new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
                new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
                new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
                new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
            };

            var queryResults = from c in customers
                               where c.Region == "Asia"
                               select c;
            Console.WriteLine("亚洲的有:");
            foreach (Customer c in queryResults)
            {
                Console.WriteLine(c);
            }

            Console.ReadLine();
        }
    }
}

查询对象的某个元素

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

namespace LinqDemo
{
    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="A",City="New York",Country="USA",Region="North America",Sales=999 },
                new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
                new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
                new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
                new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
            };

            var queryResults = from c in customers
                               where c.Region == "Asia"
                               select c.City;
            Console.WriteLine("亚洲的城市有:");
            foreach (String city in queryResults)
            {
                Console.WriteLine(city);
            }

            Console.ReadLine();
        }
    }
}

查询结果只能是字符串,或者一个对象

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

namespace LinqDemo
{
    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="A",City="New York",Country="USA",Region="North America",Sales=999 },
                new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
                new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
                new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
                new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
            };

            var queryResults = from c in customers
                               where c.Region == "Asia"
                               select new { c.City, c.Country };
            Console.WriteLine("亚洲的有:");
            foreach (Object item in queryResults)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }
    }
}

用Select函数

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

namespace LinqDemo
{
    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="A",City="New York",Country="USA",Region="North America",Sales=999 },
                new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
                new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
                new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
                new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
            };

            //var queryResults = from c in customers
            //                   where c.Region == "Asia"
            //                   select new { c.City, c.Country };
            var queryResults = customers.Where(c => c.Region == "Asia").Select(c => new { c.City, c.Country });
            Console.WriteLine("亚洲的有:");
            foreach (Object item in queryResults)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }
    }
}

去重处理Distinct方法

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

namespace LinqDemo
{
    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="A",City="New York",Country="USA",Region="North America",Sales=999 },
                new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
                new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
                new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
                new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
            };

            //var queryResults = from c in customers
            //                   where c.Region == "Asia"
            //                   select new { c.City, c.Country };
            var queryResults = customers.Select(c => c.Region).Distinct();
            Console.WriteLine("洲有:");
            foreach (Object item in queryResults)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }
    }
}

any,All查询

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

namespace LinqDemo
{
    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="A",City="New York",Country="USA",Region="North America",Sales=999 },
                new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
                new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
                new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
                new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
            };


            // Any All
            bool anyUSA = customers.Any(c => c.Country == "USA");
            if (anyUSA)
            {
                Console.WriteLine("有些人是美国的");
            }
            else
            {
                Console.WriteLine("没有美国的");
            }

            bool allAsia = customers.All(c => c.Region == "Asia");
            if (allAsia)
            {
                Console.WriteLine("全是亚洲的人");
            }
            else
            {
                Console.WriteLine("不全是亚洲的人");
            }

            Console.ReadLine();
        }
    }
}

多级排序

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

namespace LinqDemo
{
    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="A",City="New York",Country="USA",Region="North America",Sales=999 },
                new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
                new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
                new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
                new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
            };

            var queryResults = from c in customers
                               orderby c.Region,c.Country,c.City // 多级排序
                               select new {c.Region , c.Country, c.City };
            Console.WriteLine("亚洲的有:");
            foreach (Object item in queryResults)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }
    }
}

使用OrderBy,ThenBy实现

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

namespace LinqDemo
{
    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="A",City="New York",Country="USA",Region="North America",Sales=999 },
                new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
                new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
                new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
                new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
            };

            //var queryResults = from c in customers
            //                   orderby c.Region,c.Country,c.City // 多级排序
            //                   select new {c.Region , c.Country, c.City };
            var queryResults = customers.OrderBy(c => c.Region)
                .ThenBy(c => c.Country)
                .ThenBy(c => c.City)
                .Select(c => new { c.Region,c.Country, c.City});

            Console.WriteLine("亚洲的有:");
            foreach (Object item in queryResults)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }
    }
}

group的使用

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

namespace LinqDemo
{
    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="A",City="New York",Country="USA",Region="North America",Sales=999 },
                new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
                new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
                new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
                new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
            };

            var queryResults = from c in customers
                               group c by c.Region into cg
                               select new { TotalSales = cg.Sum(c => c.Sales),Region = cg.Key };

            var orderedResults =
                from cg in queryResults
                orderby cg.TotalSales descending
                select cg;


            foreach (var item in orderedResults)
            {
                Console.WriteLine(item.TotalSales+"\t:"+item.Region);
            }

            Console.ReadLine();
        }
    }
}

通过Take方法获取前几个数据
通过Skip方法,获取剩下的数据

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

namespace LinqDemo
{
    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="A",City="New York",Country="USA",Region="North America",Sales=999 },
                new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
                new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
                new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
                new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
            };

            var queryResults = from c in customers
                               group c by c.Region into cg
                               select new { TotalSales = cg.Sum(c => c.Sales),Region = cg.Key };

            var orderedResults =
                from cg in queryResults
                orderby cg.TotalSales descending
                select cg;

            // Take获取前几个数据
            // Skip获取后几个数据
            // 出去前1个后的数据
            foreach (var item in orderedResults.Skip(1))
            {
                Console.WriteLine(item.TotalSales + "\t:" + item.Region);
            }

            Console.WriteLine("----------------------------------------");

            // 前1个
            foreach (var item in orderedResults.Take(1))
            {
                Console.WriteLine(item.TotalSales + "\t:" + item.Region);
            }


            Console.ReadLine();
        }
    }
}

First与FirstOrDefault

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

namespace LinqDemo
{
    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="A",City="New York",Country="USA",Region="North America",Sales=999 },
                new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
                new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
                new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
                new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
            };

            var queryResults = from c in customers
                               group c by c.Region into cg
                               select new { TotalSales = cg.Sum(c => c.Sales),Region = cg.Key };

            var orderedResults =
                from cg in queryResults
                orderby cg.TotalSales descending
                select cg;

            // 获取第一个数据,First和FirstOrDefault
            // First方法如何数据不存在,会报错
            Console.WriteLine(orderedResults.First());

            Console.WriteLine(orderedResults.FirstOrDefault());

            Console.WriteLine(orderedResults.First(c => c.Region == "Europe"));

            Console.WriteLine(orderedResults.FirstOrDefault(c => c.Region == "Europe"));
            Console.ReadLine();
        }
    }
}

JOIN使用

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

namespace LinqDemo
{
    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 Order
    {
        public string ID { get; set; }
        public decimal Amount { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Customer> customers = new List<Customer>
            {
                new Customer {ID="A",City="New York",Country="USA",Region="North America",Sales=999 },
                new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
                new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
                new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
                new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
            };

            List<Order> orders = new List<Order>
            {
                new LinqDemo.Order {ID = "A",Amount = 100 },
                new LinqDemo.Order {ID = "A",Amount = 200 },
                new LinqDemo.Order {ID = "B",Amount = 300 },
                new LinqDemo.Order {ID = "B",Amount = 400 },
                new LinqDemo.Order {ID = "C",Amount = 500 },
                new LinqDemo.Order {ID = "D",Amount = 600 },
            };

            var queryResults = from c in customers
                               join o in orders on c.ID equals o.ID
                               select new { c.ID,c.City,SalesBefore = c.Sales,NewOrder = o.Amount,SalesAfter = c.Sales + o.Amount };

            foreach (var item in queryResults)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }
    }
}

posted @ 2017-05-13 18:27  TBHacker  阅读(2277)  评论(0编辑  收藏  举报