Lnaguage Intergrated Query(Linq) 集成查询语言

作者:追风剑情 发布于:2018-2-22 15:27 分类:C#

示例一

官方文档 https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/query-keywords

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

namespace Test10
{
    class Program
    {
        static void Main(string[] args)
        {
            // 定义数据源
            int[] scores = new int[] { 97, 92, 81, 60 };

            // 定义查询语句
            IEnumerable<int> scoreQuery =
                from score in scores
                let addscore = score + 100 //存储子表达式结果
                where score > 80
                orderby score ascending //ascending(升序) descending(降序)
                select addscore * 2;

            // 执行查询语句
            foreach (int i in scoreQuery)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine();

            // 定义数据源
            List<Student> students = new List<Student>()
            {
                new Student(){ First="A1", Last="B1", ID=1 },
                new Student(){ First="A2", Last="B2", ID=2 },
                new Student(){ First="A3", Last="B3", ID=3 },
                new Student(){ First="A4", Last="B4", ID=4 }
            };

            // 定义查询语句
            var studentQuery2 =
                from student in students
                group student by student.Last[1] into g
                orderby g.Key descending
                select g;

            // 执行查询语句
            Console.WriteLine("--------group into-----");
            foreach (var i in studentQuery2)
            {
                Console.WriteLine(string.Format("Key={0}", i.Key));
                foreach (var j in i)
                {
                    Console.WriteLine(string.Format("First={0}, Last={1}", j.First, j.Last));
                }
            }

            // 定义查询语句
            IEnumerable<ScoreInfo> studentQuery8 =
                from student in students
                where student.ID > 2
                select new ScoreInfo //返回新的数据结构
                {
                    Average = 80,
                    ID = student.ID
                };

            // 执行查询语句
            Console.WriteLine("--------select-----");
            foreach (ScoreInfo i in studentQuery8)
            {
                Console.WriteLine(string.Format("ID={0}, Average={1}", i.ID, i.Average));
            }

            // 定义数据源
            List<Category> categories = new List<Category>()
            {
                new Category(){ID = 1, Name="C1"},
                new Category(){ID = 2, Name="C2"}
            };

            List<Product> products = new List<Product>()
            {
                new Product(){CategoryID=1, Name="P1"},
                new Product(){CategoryID=2, Name="P2"},
                new Product(){CategoryID=3, Name="P3"}
            };

            // 定义查询语句
            var innerJoinQuery =
                from category in categories
                join prod in products on category.ID equals prod.CategoryID //内部联接查询
                select new { ProductName = prod.Name, Category = category.Name };

            // 执行查询语句
            Console.WriteLine("--------join in on equals-----");
            foreach (var i in innerJoinQuery)
            {
                Console.WriteLine(string.Format("ProductName={0}, Category={1}", i.ProductName, i.Category));
            }

            Console.Read();
        }
    }

    public class Student
    {
        public string First { get; set; }
        public string Last { get; set; }
        public int ID { get; set; }
    }

    public class ScoreInfo
    {
        public double Average { get; set; }
        public int ID { get; set; }
    }

    public class Category
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    public class Product
    {
        public int CategoryID { get; set; }
        public string Name { get; set; }
    }
}


运行测试

11111.jpg

标签: C#

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号