zoukankan      html  css  js  c++  java
  • 第一个Linq练习

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

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                第一个例子入门例子
                // 1. Data source.
                int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

                // 2. Query creation.
                // numQuery is an IEnumerable<int>
                var nums =
                    from num in numbers
                    where (num % 2) == 0
                    select num;

                // 3. Query execution.
                foreach (int num in nums)
                {
                    Console.Write("{0,1} ", num);
                }

                Console.Write("{0,1} ", nums.First());


                Console.ReadLine();

                简单范型结构

                int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

                List<int> ints =
                    (from num in numbers
                    where (num % 2) == 0
                    select num).ToList();

                //var numQuery3 = (from num in numbers where (num % 2) == 0 select num).ToArray();

                foreach (int i in ints)
                {
                    Console.WriteLine(i);
                }


                Console.ReadLine();


                对象范型结构


                List<employee> employees = new List<employee>();

                employees.Add(new employee("张三", 16));
                employees.Add(new employee("张四", 17));
                employees.Add(new employee("张五", 18));
                employees.Add(new employee("张六", 19));
                employees.Add(new employee("李四", 16));
                employees.Add(new employee("王五", 16));

                //另一种写法
                //List<employee> customerQuery =
                //    (from age16 in employees
                //     where age16.Age == 16
                //     select age16).ToList();

                IEnumerable<employee> customerQuery =
                    from age16 in employees
                     where age16.Age == 16
                     select age16;


                foreach (employee mye in customerQuery)
                {
                    Console.WriteLine(mye.Name+mye.Age);  
                }

                Console.ReadLine();

                合并的例子
                List<Student> students = new List<Student>()
                {
                    new Student { First = "Svetlana", Last = "Omelchenko", ID = 111, Street = "123 Main Street", City = "Seattle", Scores = new List<int> { 97, 92, 81, 60 } },
                    new Student { First = "Claire", Last = "O’Donnell", ID = 112, Street = "124 Main Street", City = "Seattle", Scores = new List<int> { 75, 84, 91, 39 } },
                    new Student { First = "Sven", Last = "Mortensen", ID = 113, Street = "125 Main Street", City = "Lake City", Scores = new List<int> { 88, 94, 65, 91 } }, };      
                // Create the second data source.
                List<Teacher> teachers = new List<Teacher>()      
                {                           
                    new Teacher {First="Ann", Last="Beebe", ID=945, City = "Seattle"},          
                    new Teacher {First="Alex", Last="Robinson", ID=956, City = "Redmond"},           
                    new Teacher {First="Michiyo", Last="Sato", ID=972, City = "Tacoma"}        };       
                // Create the query.       
                var peopleInSeattle =
                    (from student in students
                     where student.City == "Seattle"
                     select new { fn=student.First, ln=student.Last }).Concat(from teacher in teachers
                                                                        where teacher.City == "Seattle"
                                                                        select new { fn=teacher.First, ln=teacher.Last });
                Console.WriteLine("The following students and teachers live in Seattle:");      
                // Execute the query.       
                foreach (var person in peopleInSeattle)       
                {          
                    Console.WriteLine(person);       
                }  
                Console.WriteLine("Press any key to exit.");       
                Console.ReadKey();   


                元素操作
                 Data source.       
                double[] radii = { 1, 2, 3 };       
                 Query.       
                IEnumerable<string> query =           
                    from rad in radii           
                    select String.Format("Area = {0}", (rad * rad) * 3.14);       
                 Query execution.        
                foreach (string s in query)           
                    Console.WriteLine(s);       
                 Keep the console open in debug mode.       
                Console.WriteLine("Press any key to exit.");       
                Console.ReadKey();

                对象toXML
                // Create the data source by using a collection initializer.      
                List<Student> students = new List<Student>()       
                {           
                    new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores = new List<int>{97, 92, 81, 60}},           
                    new Student {First="Claire", Last="O’Donnell", ID=112, Scores = new List<int>{75, 84, 91, 39}},          
                    new Student {First="Sven", Last="Mortensen", ID=113, Scores = new List<int>{88, 94, 65, 91}},      
                };       
               
                // Create the query.       
                var studentsToXML = new XElement("Root",          
                    from student in students           
                    let x = String.Format("{0},{1},{2},{3}", student.Scores[0],student.Scores[1],student.Scores[2], student.Scores[3])          
                    select new XElement("student",                      
                        new XElement("First", student.First),                     
                        new XElement("Last", student.Last),                     
                        new XElement("Scores", x)                  
                        )
                        // end "student"               
                        );
                        // end "Root"      
                        // Execute the query.      
                Console.WriteLine(studentsToXML);

                // Keep the console open in debug mode.      
                Console.WriteLine("Press any key to exit.");       
                Console.ReadKey();   
            }
        }

        public class employee
        {

            public string Name
            {
                get;
                set;
            }

            public int Age
            {
                get;
                set;
            }

            public employee(string name,int age)
            {
                Name = name;
                Age = age;
            }
        }

        class Student
        {
            public string First { get; set; }
            public string Last { get; set; }
            public int ID { get; set; }
            public string Street { get; set; }
            public string City { get; set; }
            public List<int> Scores;}

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

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    vs2005设置断点不能调试问题(方法三为首选项,一般都可以解决)
    SQL中内连接和外连接的问题!
    javascript读写删cookie的简单方法
    数据库语句 select * from table where 1=1 的用法和作用
    gridview 和repeater 添加序号的方法
    asp.net Forms身份验证详解(转载)
    Asp.net中的认证与授权(转载)
    ASP.NET中前台javascript与后台代码调用
    android 模拟器不能上网解决方法
    大数据量系统架构
  • 原文地址:https://www.cnblogs.com/starcrm/p/1361641.html
Copyright © 2011-2022 走看看