标签:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 var patent1 = new 6 { 7 Title = "xxm1", 8 YearOfPublication = "1977" 9 }; 10 var patent2 = new 11 { 12 Title = "xxm2", 13 YearOfPublication = "1978" 14 }; 15 var patent3 = new 16 { 17 patent1.Title, 18 //重新命名属性 19 Year = patent2.YearOfPublication 20 }; 21 22 Console.WriteLine(patent1.Title + ":" + patent1.YearOfPublication); 23 Console.WriteLine(patent2.Title + ":" + patent2.YearOfPublication); 24 Console.WriteLine(patent3.Title + ":" + patent3.Year); 25 26 Console.WriteLine(); 27 Console.WriteLine(patent1); 28 Console.WriteLine(patent2); 29 Console.WriteLine(patent3); 30 31 Console.ReadLine(); 32 33 34 35 } 36 } 37 38 输出: 39 xxm1:1977 40 xxm2:1978 41 xxm1:1978 42 43 { Title = xxm1, YearOfPublication = 1977 } 44 { Title = xxm2, YearOfPublication = 1978 } 45 { Title = xxm1, Year = 1978 }
1 string text=" this is a test of the ..."; 2 //<====> 3 var text="this is a test of the ...";
1 var patent3 = new 2 { 3 patent1.Title, 4 //重新命名属性 5 Year = patent2.YearOfPublication 6 };
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 List<string> sevenWorldBlunders = new List<string>(); 7 sevenWorldBlunders = new List<string>() 8 { 9 "Wealth without work", 10 "Pleasure without conscience", 11 "Knowledge without character", 12 }; 13 14 15 Print(sevenWorldBlunders); 16 17 } 18 private static void Print<T>(IEnumerable<T> items) 19 { 20 foreach (T item in items) 21 { 22 Console.WriteLine(item); 23 } 24 25 } 26 }
1 int[] arr = new[] { 1, 2, 3, 4, 5 }; 2 3 foreach (int item in arr) 4 { 5 Console.WriteLine(item); 6 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 IEnumerable<Patent> patents = PatentData.Patents; 6 7 Print(patents); 8 9 Console.WriteLine(); 10 11 IEnumerable<Inventor> inventors = PatentData.Inventors; 12 13 Print(inventors); 14 15 Console.ReadLine(); 16 17 18 19 20 } 21 public static void Print<T>(IEnumerable<T> items) 22 { 23 foreach (T item in items) 24 { 25 Console.WriteLine(item); 26 } 27 } 28 //专利类 29 public class Patent 30 { 31 public string Title { get; set; } 32 33 public string YearOfPublication { get; set; } 34 public string ApplicationNumber { get; set; } 35 public long[] InventorIds { get; set; } 36 public override string ToString() 37 { 38 return string.Format("{0}({1})", Title, YearOfPublication); 39 } 40 } 41 //发明者类 42 public class Inventor 43 { 44 public long Id { get; set; } 45 public string Name { get; set; } 46 public string City { get; set; } 47 public string State { get; set; } 48 public string Country { get; set; } 49 public override string ToString() 50 { 51 return string.Format("{0}({1},{2})", Name, City, State); 52 } 53 } 54 55 //实际数据 56 public static class PatentData 57 { 58 public static readonly Inventor[] Inventors = new Inventor[] { 59 new Inventor(){ 60 Name="Benjamin Franklin",City="Philadelphia", 61 State="PA",Country="USA",Id=1 62 }, 63 new Inventor(){ 64 Name="Orville Wright",City="Kitty Hawk", 65 State="NC",Country="USA",Id=2 66 }, 67 new Inventor(){ 68 Name="Wilbur Wright",City="Kitty Hawk", 69 State="NC",Country="USA",Id=3 70 }, 71 new Inventor(){ 72 Name="Samuel Morse",City="New York", 73 State="NY",Country="USA",Id=4 74 }, 75 new Inventor(){ 76 Name="George Stephenson",City="Wylam", 77 State="Northumberland",Country="UK",Id=5 78 }, 79 new Inventor(){ 80 Name="John Michaelis",City="Chicago", 81 State="IL",Country="USA",Id=6 82 }, 83 new Inventor(){ 84 Name="Mary Phelps Jacob",City="New York", 85 State="NY",Country="USA",Id=7 86 }, 87 }; 88 89 public static readonly Patent[] Patents = new Patent[] { 90 new Patent(){ 91 Title="Bifocals",YearOfPublication="1784", 92 InventorIds=new long[]{1} 93 }, 94 new Patent(){ 95 Title="Phonograph",YearOfPublication="1877", 96 InventorIds=new long[]{1} 97 }, 98 new Patent(){ 99 Title="Kinetoscope",YearOfPublication="1888", 100 InventorIds=new long[]{1} 101 }, 102 new Patent(){ 103 Title="Electrical Telegraph",YearOfPublication="1837", 104 InventorIds=new long[]{4} 105 }, 106 new Patent(){ 107 Title="Flying machine",YearOfPublication="1903", 108 InventorIds=new long[]{2,3} 109 }, 110 new Patent(){ 111 Title="Steam Locomotive",YearOfPublication="1815", 112 InventorIds=new long[]{5} 113 }, 114 new Patent(){ 115 Title="Droplet deposition apparatus",YearOfPublication="1989", 116 InventorIds=new long[]{6} 117 }, 118 new Patent(){ 119 Title="Backless Brassiere",YearOfPublication="1914", 120 InventorIds=new long[]{7} 121 }, 122 }; 123 } 124 }
1 IEnumerable<Patent> patents = PatentData.Patents; 2 3 patents = patents.Where( 4 patent => patent.YearOfPublication.StartsWith("18") 5 ); 6 7 Print(patents);
1 IEnumerable<Patent> patents = PatentData.Patents; 2 3 IEnumerable<Patent> patents1800 = patents.Where( 4 patent => patent.YearOfPublication.StartsWith("18") 5 ); 6 IEnumerable<string> items = patents1800.Select(item => item.ToString()); 7 8 //Print(patents); 9 Print(items);
1 IEnumerable<string> filelist = Directory.GetFiles("D:\\"); 2 IEnumerable<FileInfo> files = filelist.Select(file => new FileInfo(file)); 3 Print(files); 4 //注:以上的Lambda表达的形式参数的类型都与集合中的元素类型一致。 5 匿名类型: 6 IEnumerable<string> filelist = Directory.GetFiles("D:\\"); 7 var items = filelist.Select(file => 8 { 9 FileInfo fileInfo = new FileInfo(file); 10 return new { FileName = fileInfo.Name, Size = fileInfo.Length }; 11 }); 12 Print(items);
1 IEnumerable<string> filelist = Directory.GetFiles("D:\\"); 2 var items = filelist.AsParallel().Select(file => 3 { 4 FileInfo fileInfo = new FileInfo(file); 5 return new { FileName = fileInfo.Name, Size = fileInfo.Length }; 6 }); 7 Print(items);
1 IEnumerable<Patent> patents = PatentData.Patents; 2 Console.WriteLine("Patent Count:{0}", patents.Count()); 3 Console.WriteLine("Patent Count in 1800s:{0}", 4 patents.Count( 5 patent => patent.YearOfPublication.StartsWith("18") 6 ));
1 if(patents.Any()) 2 { }
1 IEnumerable<Patent> patents = PatentData.Patents; 2 bool result; 3 patents = patents.Where(patent => 4 { 5 if (result = patent.YearOfPublication.StartsWith("18")) 6 { 7 Console.WriteLine("Where StartsWith 18 :" + patent); 8 } 9 return result; 10 }); 11 Console.WriteLine("1. Patents prior to the 1900s are:"); 12 foreach (Patent patent in patents) 13 { 14 15 } 16 Console.WriteLine(); 17 Console.WriteLine("2. A second listing of patents prior to the 1900s:"); 18 Console.WriteLine(" There are {0} patents prior to 1900.", patents.Count()); 19 20 Console.WriteLine(); 21 Console.WriteLine("3. A third listing of patents prior to the 1900s:"); 22 patents = patents.ToArray(); 23 Console.Write(" There are "); 24 Console.WriteLine("{0} patents prior to 1900.", patents.Count());
1 IEnumerable<Patent> items = null; 2 Patent[] patents = PatentData.Patents; 3 4 items = patents.OrderBy(patent => patent.YearOfPublication) 5 .ThenBy(patent => patent.Title); 6 Print(items); 7 8 Console.WriteLine(); 9 10 items = patents.OrderByDescending(patent => patent.YearOfPublication) 11 .ThenByDescending(patent => patent.Title); 12 Print(items); 13
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 Department[] departments = CorporateData.Departments; 7 Employee[] employees = CorporateData.Employees; 8 9 var items = employees.Join( 10 departments,//关联的数据 11 employee => employee.DepartmentId,//联接时的参考属性 12 department => department.Id,//联接时的参考属性 13 (employee, department) => new //返回的数据 14 { 15 employee.Id, 16 employee.Name, 17 employee.Title, 18 Department = department 19 20 }); 21 Print(items); 22 23 Console.ReadLine(); 24 25 26 27 28 } 29 public static void Print<T>(IEnumerable<T> items) 30 { 31 foreach (T item in items) 32 { 33 Console.WriteLine(item); 34 } 35 } 36 37 38 //部门类 39 public class Department 40 { 41 public long Id { get; set; } 42 public string Name { get; set; } 43 public override string ToString() 44 { 45 return string.Format("{0}", Name); 46 } 47 } 48 49 //员工类 50 public class Employee 51 { 52 public int Id { get; set; } 53 public string Name { get; set; } 54 public string Title { get; set; } 55 public int DepartmentId { get; set; } 56 public override string ToString() 57 { 58 return string.Format("{0}({1})", Name, Title); 59 } 60 } 61 62 //实际数据 63 public static class CorporateData 64 { 65 public static readonly Department[] Departments = new Department[] { 66 new Department() 67 { 68 Name="Corporate",Id=0 69 }, 70 new Department() 71 { 72 Name="Corporate",Id=1 73 }, 74 new Department() 75 { 76 Name="Engineering",Id=2 77 }, 78 new Department() 79 { 80 Name="Information Technology",Id=3 81 }, 82 new Department() 83 { 84 Name="Research",Id=4 85 }, 86 new Department() 87 { 88 Name="Marketing",Id=5 89 }, 90 }; 91 public static readonly Employee[] Employees = new Employee[] { 92 new Employee() 93 { 94 Name="Mark Michaelis", 95 Title="Chief Computer Nerd", 96 DepartmentId=0 97 }, 98 new Employee() 99 { 100 Name="Michael Stokesbary", 101 Title="Senior Computer Wizard", 102 DepartmentId=2 103 }, 104 new Employee() 105 { 106 Name="Brian Jones", 107 Title="Enterprise Integration Guru", 108 DepartmentId=2 109 }, 110 new Employee() 111 { 112 Name="Jewel Floch", 113 Title="Bookkeeper Extraordinaire", 114 DepartmentId=1 115 }, 116 new Employee() 117 { 118 Name="Robert Stokesbary", 119 Title="Expert Mainframe Engineer", 120 DepartmentId=3 121 }, 122 new Employee() 123 { 124 Name="Paul R.Bramsman", 125 Title="Programmer Extraordinaire", 126 DepartmentId=2 127 }, 128 new Employee() 129 { 130 Name="Thomas Heavey", 131 Title="Software Architect", 132 DepartmentId=2 133 }, 134 new Employee() 135 { 136 Name="John Michaelis", 137 Title="Inventor", 138 DepartmentId=4 139 }, 140 141 }; 142 } 143 144 } 145 146 输出: 147 { Id = 0, Name = Mark Michaelis, Title = Chief Computer Nerd, Department = Corpo 148 rate } 149 { Id = 0, Name = Michael Stokesbary, Title = Senior Computer Wizard, Department 150 = Engineering } 151 { Id = 0, Name = Brian Jones, Title = Enterprise Integration Guru, Department = 152 Engineering } 153 { Id = 0, Name = Jewel Floch, Title = Bookkeeper Extraordinaire, Department = Co 154 rporate } 155 { Id = 0, Name = Robert Stokesbary, Title = Expert Mainframe Engineer, Departmen 156 t = Information Technology } 157 { Id = 0, Name = Paul R.Bramsman, Title = Programmer Extraordinaire, Department 158 = Engineering } 159 { Id = 0, Name = Thomas Heavey, Title = Software Architect, Department = Enginee 160 ring } 161 { Id = 0, Name = John Michaelis, Title = Inventor, Department = Research }
1 IEnumerable<Employee> employees = CorporateData.Employees; 2 IEnumerable<Department> departments = CorporateData.Departments; 3 4 IEnumerable<IGrouping<int, Employee>> groupedEmployees = 5 employees.GroupBy(employee => employee.DepartmentId); 6 foreach (IGrouping<int, Employee> employeeGroup in groupedEmployees) 7 { 8 Console.WriteLine(); 9 foreach (Employee employee in employeeGroup) 10 { 11 Console.WriteLine("\t"+employee); 12 } 13 Console.WriteLine("\t Count:"+employeeGroup.Count()); 14 } 15
1 IEnumerable<Employee> employees = CorporateData.Employees; 2 IEnumerable<Department> departments = CorporateData.Departments; 3 4 5 var items = departments.GroupJoin( 6 employees,//关联的对象数组 7 department => department.Id,//关联的键 8 employee => employee.DepartmentId,//关联的键 9 (department, departmentEmployees) => new//返回的数据 10 { 11 department.Id, 12 department.Name, 13 Employees = departmentEmployees 14 }); 15 foreach (var item in items) 16 { 17 Console.WriteLine("部门:{0}", item.Name+":"); 18 foreach (Employee employee in item.Employees) 19 { 20 Console.WriteLine("\t"+employee); 21 } 22 }
1 string[] text = { "a b", "c d", "e f" }; 2 var items = text.Select( 3 item => item.Split(‘ ‘) 4 ); 5 foreach (string[] strs in items) 6 { 7 foreach (string str in strs) 8 { 9 Console.WriteLine("\t"+str); 10 } 11 } 12 换成SelectMany 13 string[] text = { "a b", "c d", "e f" }; 14 var items = text.SelectMany( 15 item => item.Split(‘ ‘) 16 ); 17 foreach (var item in items) 18 { 19 Console.WriteLine("\t" + item); 20 }
标签:
原文地址:http://www.cnblogs.com/tlxxm/p/4674988.html