第一个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; } 
    }
}
目前维护的开源产品:https://gitee.com/475660
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号