首页 > 代码库 > linq 一栏表

linq 一栏表

 

嘿嘿,其实就是一张图:

 

 

技术分享

 

将多个输入联接到一个输出序列

   class Student
    {
        public string Name { get; set; }

        public int Age { get; set; }

        public string City { get; set; }

        public List<int> Scores { get; set; }
    }

    class Teacher
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public string City { get; set; }

    }

操作代码:

public void Concat()
        {
            //创建第一个数据源
            var students = new List<Student>()
            {
                new Student()
                {
                    Age = 23,
                    City = "广州",
                    Name = "小C",
                    Scores = new List<int>(){85,88,83,97}
                },
                new Student()
                {
                    Age = 18,
                    City = "广西",
                    Name = "小明",
                    Scores = new List<int>(){86,78,85,90}
                },
                new Student()
                {
                    Age = 33,
                    City = "梦里",
                    Name = "小叁",
                    Scores = new List<int>(){86,68,73,97}
                }
            };

            //创建第二个数据源
            var teachers = new List<Teacher>()
            {
                new Teacher()
                {
                    Age = 35,
                    City = "梦里",
                    Name = "啵哆"
                },
                new Teacher()
                {
                    Age = 28,
                    City = "云南",
                    Name = "小红"
                },
                new Teacher()
                {
                    Age = 38,
                    City = "河南",
                    Name = "丽丽"
                }
            };

            //创建我们的contact
            var concat = (from s in students
                          where s.City == "新都"
                          select s.Name
                        ).Concat(
                         from t in teachers
                         where t.City == "重庆"
                         select t.Name
                        );
            //使用我们concat将我们的两个数据集联系在一起滴呀;效果很好的;
        }

 

2.将内存中的对象转换为 XML

            var students = new List<Student>()
            {
                new Student()
                {
                    Age = 18,
                    Name = "小A",
                    Scores = new List<int>() {88,85,74,66 }
                },
                new Student()
                {
                    Age = 35,
                    Name = "小B",
                    Scores = new List<int>() {88,85,74,66 }
                },
                new Student()
                {
                    Age = 28,
                    Name = "小啥",
                    Scores = new List<int>() {88,85,74,66 }
                }
            };

            //创建查询
            var studentsToXml = new XElement("Root",
                from student in students
                let x = $"{student.Scores[0]},{student.Scores[1]},{student.Scores[2]},{student.Scores[3]}"
                select new XElement("student",
                new XElement("Name", student.Name),
                new XElement("Age", student.Age),
                new XElement("Scores", x))
            )

 

这里捋一捋这个let的使用滴呀;

在Linq查询中Let子句可以创建一个新的范围变量,并使用该变量保存表达式的结

$"{radius * radius * 3.14}" 相当于 string.Format("{0}",radius * radius * 3.14),这里采用的是 C# 6.0 的语法

 

linq 一栏表