#眉標=LINQ #副標=剖析LINQ運作機制(2) #大標=回到起點─LINQ語法規則 #作者=文圖/黃忠成 ==<反灰>=========== from in <|||> ================ ==<反灰>=========== string[] list = new string[] { "1111", "2222", "3333" }; var p = from o in list select o; ================ ==<反灰>=========== string[] list = new string[] { "1111", "2222", "3333" }; var p = from o in list where o == "2222" select o; ================ ==程式1=========== private static void TestComplexWhere2() { var p1 = new []{new {Name = "code6421", Address = "Taipen"}, new {Name = "tom", Address = "Taipen"}, new {Name = "jeffray", Address = "NY"}}; var p2 = new []{new {Name = "code6421", Title = "Manager"}, new {Name = "tom", Title = "Director"}, new {Name = "jeffray", Title = "Programmer"}}; var p3 = new[]{new {Name = "code6421", Hand = "Right", Address = "Taipen"}, new {Name = "tom", Hand = "Right", Address = "Taipen"}, new {Name = "jeffray", Hand = "Left", Address = "Taipen"}}; var p6 = from s in p2 where (from s1 in p1 where s1.Name == s.Name select s1.Address).ToArray()[0] == (from s2 in p3 where s2.Name == s.Name select s2.Address).ToArray()[0] select s; foreach (var i in p6) Console.WriteLine(i.Name); Console.ReadLine(); } ================ ==程式2 =========== private static void TestGroupByLinq() { Person[] persons = new Person[]{ new Person{Name="code6421",Age=18,Address="Taipen"}, new Person{Name="jeffray",Age=18,Address="USA"}, new Person{Name="catch",Age=12,Address="USA"}, new Person{Name="joe",Age=18,Address="TY"}}; var p = from o in persons group o by o.Address into g select new { Address = g.Key, Persons = g }; foreach (var s in p) { Console.WriteLine("Group : {0}", s.Address); foreach (var s1 in s.Persons) Console.WriteLine(s1.Name); } Console.ReadLine(); } ================ ==程式3=========== Group : Taipen code6421 Group : USA jeffray catch Group : TY Joe ================ ==程式4=========== private static void TestComplexGroupBy2() { var p1 = new[]{new {Name = "code6421", Address = "Taipen"}, new {Name = "tom", Address = "Taipen"}, new {Name = "jeffray", Address = "NY"}}; var p2 = new[]{new {Name = "code6421", Hand = "Right", Address = "Taipen"}, new {Name = "tom", Hand = "Right", Address = "NY"}, new {Name = "jeffray", Hand = "Left", Address = "Taipen"}}; var p6 = from s in p2 group s by (from s1 in p2 where s.Name == s1.Name select s1.Address).ToArray()[0] into g select new { Key = g.Key, Persons = g }; foreach (var i in p6) { Console.WriteLine(" {0}", i.Key); foreach (var v in i.Persons) Console.WriteLine(v.Name); } Console.ReadLine(); } ================ ==程式5=========== Taipen code6421 jeffray NY Tom ================ ==程式6=========== private static void TestJoin() { var p1 = new[]{new {Name = "code6421", Address = "Taipen"}, new {Name = "tom", Address = "Taipen"}, new {Name = "jeffray", Address = "NY"}}; var p2 = new[]{new {Name = "code6421", Title = "Manager"}, new {Name = "tom", Title = "Director"}, new {Name = "jeffray", Title = "Programmer"}}; var p3 = from s in p1 join s1 in p2 on s.Name equals s1.Name select new { Name = s.Name, Address = s.Address, Title = s1.Title }; foreach (var v in p3) Console.WriteLine("Name {0}, Address {1}, Title {2}", v.Name, v.Address, v.Title); Console.ReadLine(); } ================ ==程式7=========== Name code6421, Address Taipen, Title Manager Name tom, Address Taipen, Title Director Name jeffray, Address NY, Title Programmer ================ ==程式8=========== private static void TestJoin2() { var p1 = new[]{new {Name = "code6421", Address = "Taipen"}, new {Name = "tom", Address = "Taipen"}, new {Name = "jeffray", Address = "NY"}}; var p2 = new[]{new {Name = "code6421", Title = "Manager"}, new {Name = "tom", Title = "Director"}, new {Name = "jeffray", Title = "Programmer"}}; var p3 = new[]{new {Name = "code6421", Hand = "Right"}, new {Name = "tom", Hand = "Right"}, new {Name = "jeffray", Hand = "Left"}}; var p4 = from s in p1 join s1 in p2 on s.Name equals s1.Name join s2 in p3 on s.Name equals s2.Name select new { Name = s.Name, Address = s.Address, Title = s1.Title,Hand = s2.Hand }; foreach (var v in p4) Console.WriteLine("Name {0}, Address {1}, Title {2}, Hand {3}", v.Name, v.Address, v.Title,v.Hand); Console.ReadLine(); } ================ ==程式9=========== Name code6421, Address Taipen, Title Manager, Hand Right Name tom, Address Taipen, Title Director, Hand Right Name jeffray, Address NY, Title Programmer, Hand Left ================ ==程式10=========== private static void TestOrderLinq() { string[] list = new string[] { "1111", "2222", "3333" }; var p = from o in list orderby o descending select o; foreach (var s in p) Console.WriteLine(s); Console.ReadLine(); } ================ ==程式11=========== 3333 2222 1111 ================ ==程式12=========== var p3 = from s in p1 join s1 in p2 on s.Name equals s1.Name select new { Name = s.Name, Address = s.Address, Title = s1.Title }; ================ ==程式13 =========== Private static void TestSelectDistinct() { var p1 = new[]{ new {Name = "code6421", Age=18, Address = "Taipen"}, new {Name = "cathy", Age=18, Address = "Taipen"}, new {Name = "tom", Age=18, Address = "NY"}}; var p2 = (from s in p1 select s.Address).Distinct(); foreach (var p4 in p2) Console.WriteLine(p4); Console.ReadLine(); } ================ ==程式14 =========== Taipen NY ================ ==程式15=========== Private static void TestSelectMany() { var p1 = new []{ new {Name = "code6421", Age=18, Address = "Taipen"}, new {Name = "cathy", Age=18, Address = "Taipen"}, new {Name = "tom", Age=18, Address = "NY"}}; var p2 = new []{ new {Title="Manager"}, new {Title="Programmer"}, new {Title="SA"}}; var p3 = from s1 in p1 from s2 in p2 select new {s1.Name,s2.Title}; foreach (var p4 in p3) Console.WriteLine(string.Format("Name {0}, Title {1}", p4.Name, p4.Title)); Console.ReadLine(); } ================ ==程式16 =========== Name code6421, Title Manager Name code6421, Title Programmer Name code6421, Title SA Name cathy, Title Manager Name cathy, Title Programmer Name cathy, Title SA Name tom, Title Manager Name tom, Title Programmer Name tom, Title SA ================ ==程式17 =========== private static void TestIndexed() { var p1 = new[]{ new {Name = "code6421", Age=18, Address = "Taipen"}, new {Name = "cathy", Age=18, Address = "Taipen"}, new {Name = "tom", Age=18, Address = "NY"}, new {Name = "tom2", Age=18, Address = "NY"}}; var p3 = (from s1 in p1 select s1.Name[1]); foreach(var p4 in p3) Console.WriteLine(p4); Console.ReadLine(); } ================ ==程式18 =========== private static void TestTake() { var p1 = new[]{ new {Name = "code6421", Age=18, Address = "Taipen"}, new {Name = "cathy", Age=18, Address = "Taipen"}, new {Name = "tom", Age=18, Address = "NY"}}; var p3 = (from s1 in p1 select s1).Take(2); foreach (var p4 in p3) Console.WriteLine(string.Format("Name {0}, Title {1}", p4.Name, p4.Address)); Console.ReadLine(); } ================ ==程式19 =========== private static void TestTakeWhile() { var p1 = new[]{ new {Name = "code6421", Age=18, Address = "Taipen"}, new {Name = "cathy", Age=18, Address = "Taipen"}, new {Name = "tom", Age=18, Address = "NY"}}; var p3 = (from s1 in p1 select s1).TakeWhile(x => x.Address == "Taipen"); foreach (var p4 in p3) Console.WriteLine(string.Format("Name {0}, Title {1}", p4.Name, p4.Address)); Console.ReadLine(); } ================ ==程式20 =========== private static void TestSkip() { var p1 = new[]{ new {Name = "code6421", Age=18, Address = "Taipen"}, new {Name = "cathy", Age=18, Address = "Taipen"}, new {Name = "tom", Age=18, Address = "NY"}}; //skip will skip (count) at collections. var p3 = (from s1 in p1 select s1).Skip(1); foreach (var p4 in p3) Console.WriteLine(string.Format("Name {0}, Title {1}", p4.Name, p4.Address)); Console.ReadLine(); } ================ ==程式21 =========== private static void TestSkipWhile() { var p1 = new[]{ new {Name = "code6421", Age=18, Address = "Taipen"}, new {Name = "cathy", Age=18, Address = "Taipen"}, new {Name = "tom", Age=18, Address = "NY"}, new {Name = "tom2", Age=18, Address = "NY"}}; var p3 = (from s1 in p1 select s1).SkipWhile(x => x.Address == "Taipen"); foreach (var p4 in p3) Console.WriteLine(string.Format("Name {0}, Address {1}", p4.Name, p4.Address)); Console.ReadLine(); } ================ ==程式22 =========== Private static void TestFirst() { var p1 = new[]{ new {Name = "code6421", Age=18, Address = "Taipen"}, new {Name = "cathy", Age=18, Address = "Taipen"}, new {Name = "tom", Age=18, Address = "NY"}, new {Name = "tom2", Age=18, Address = "NY"}}; var p3 = (from s1 in p1 select s1).First(x => x.Address == "NY"); Console.WriteLine(string.Format("Name {0}, Address {1}", p3.Name, p3.Address)); Console.ReadLine(); } ================ ==程式23 =========== private static void TestToDictionary() { var p1 = new[]{ new {Name = "code6421", Age=18, Address = "Taipen"}, new {Name = "cathy", Age=18, Address = "Taipen"}, new {Name = "tom", Age=18, Address = "NY"}}; var p3 = (from s1 in p1 select s1).ToDictionary(x => x.Name); foreach (var p4 in p3) { Console.WriteLine("Key {0}", p4.Key); Console.WriteLine("Name {0}", p4.Value.Address); } Console.ReadLine(); } ================ ==程式24 =========== private static void TestUnion() { var p1 = new int[]{ 1 ,3 ,5, 7 ,9, 11}; var p2 = new int[]{ 2 ,4 ,6, 8 , 10, 11}; var p3 = from s in p1 select s; var p4 = from s in p2 select s; var p5 = p3.Union(p4); foreach (var i in p5) Console.WriteLine(i); Console.ReadLine(); } ================ ==程式25 =========== 1 3 5 7 9 11 2 4 6 8 10 ================ ====程式26 =========== private static void TestUnion2() { var p1 = new string[] { "code6421", "tom", "Cathy" }; var p2 = new string[] { "code6421", "tom2", "cathy" }; var p3 = p1.Union(p2,StringComparer.Create(System.Globalization.CultureInfo.CurrentCulture,true)); foreach(var p4 in p3) Console.WriteLine(p4); Console.ReadLine(); } ================ ==程式27 =========== code6421 tom Cathy tom2 ================ ==程式28 =========== private static void TestIntersect() { var p1 = new int[] { 1, 3, 5, 7, 9, 11 }; var p2 = new int[] { 2, 4, 6, 8, 10, 11 }; var p3 = from s in p1 select s; var p4 = from s in p2 select s; var p5 = p3.Intersect(p4); foreach (var i in p5) Console.WriteLine(i); Console.ReadLine(); } ================ ==程式29=========== Private static void TestIntersect2() { var p1 = new string[] { "code6421", "tom", "Cathy" }; var p2 = new string[] { "code6421", "tom2", "cathy" }; var p3 = p1.Intersect(p2, StringComparer.Create( System.Globalization.CultureInfo.CurrentCulture, true)); foreach (var p4 in p3) Console.WriteLine(p4); Console.ReadLine(); } ================ ==程式30 =========== code6421 Cathy ================ ==程式31 =========== Private static void TestExpect() { var p1 = new int[] { 1, 3, 5, 7, 9, 11 }; var p2 = new int[] { 2, 4, 6, 8, 10, 11 }; var p3 = from s in p1 select s; var p4 = from s in p2 select s; var p5 = p3.Except(p4); foreach (var i in p5) Console.WriteLine(i); Console.ReadLine(); } ================ ==程式32 =========== 1 3 5 7 9 ================ ==程式33 =========== private static void TestExpect2() { var p1 = new string[] { "code6421", "tom", "Cathy" }; var p2 = new string[] { "code6421", "tom2", "cathy" }; var p3 = from s in p1 select s; var p4 = from s in p2 select s; var p5 = p3.Except(p4,StringComparer.Create(System.Globalization.CultureInfo.CurrentCulture,true)); foreach (var i in p5) Console.WriteLine(i); Console.ReadLine(); } ================ ==程式34 =========== private static void TestOfType() { ArrayList list = new ArrayList(); list.Add("Test1"); list.Add("Test2"); list.Add("Test3"); list.Add("Test4"); list.Add(16); var p2 = list.OfType(); //OfType will not raise invalid type-casting exception, //if you need receive the exception,use Cast. foreach (var p3 in p2) Console.WriteLine("Type is {0} and value is {1}",p3.GetType(),p3); Console.ReadLine(); } ================ ==程式35 =========== Type is System.String and value is Test1,Type is System.String and value is Test2 Type is System.String and value is Test3,Type is System.String and value is Test4 ================ ==程式36 =========== private static void TestSum1() { var p1 = new[]{18,20,25}; var p3 = p1.Sum(); Console.WriteLine(p3); Console.ReadLine(); } ================ ==程式37 =========== private static void TestSum2() { var p1 = new[]{new {Name = "code6421", Age = 18}, new {Name = "tom", Age = 20}, new {Name = "jeffray", Age = 25}}; var p2 = (from s in p1 select s).Sum(x => x.Age); Console.WriteLine(p2); Console.ReadLine(); } ================ ==程式38 =========== private static void TestAggregate() { var p1 = new[] { 18, 20, 25 }; var p3 = p1.Aggregate((x, y) => x * y); Console.WriteLine(p3); Console.ReadLine(); } ================ ==程式39 =========== private static void TestAggregate2() { var p1 = new[] { 18, 20, 25 }; var p3 = p1.Aggregate(9000,(x, y) => x * y); Console.WriteLine(p3); Console.ReadLine(); } ================ ==程式40 =========== var p1 = new[] { "code6421", "tom", "cathy" }; var p5 = p1.Where(x => { bool result = false; SqlConnection conn = new SqlConnection("...."); conn.Open(); return result; }); ================