#眉標=LINQ #副標=剖析LINQ運作機制(1) #大標=LINQ To Object Framework #作者=文圖/黃忠成 ==程式1=========== private static void TestSimpleLinq() { string[] list = new string[] { "1111", "2222", "3333" }; var p = from o in list select o; foreach (var s in p) Console.WriteLine(s); }================ ==程式2 =========== private static void TestConditionLinq() { string[] list = new string[] { "1111", "2222", "3333" }; var p = from o in list where o == "2222" select o; foreach (var s in p) Console.WriteLine(s); }================ ==程式3 =========== var p = from o in list where o like "1%" select o;================ ==程式4 =========== var p = from o in list where o.Contains("2") select o; ================ ==程式5 =========== private static void TestConditionLinq() { IEnumerable p = new string[] { "1111", "2222", "3333" }.Where(delegate (string o) { return o == "2222"; }); foreach (string s in p) { Console.WriteLine(s); } Console.ReadLine(); }================ ==程式7 =========== static void TestImplicitLocalVariable() { var vint = 10; var vstring = "TEST"; var vint64 = 9029349442; var vdouble = 9.234; Console.WriteLine("{0},{1},{2},{3}", vint.GetType().ToString(), vstring.GetType().ToString(), vint64.GetType().ToString(), vdouble.GetType().ToString()); Console.ReadLine(); }================ ==程式8 =========== class Program { private static var t = 15; static void TestImplicitLocalVariable(var t) {} }================ ==程式9 =========== using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyExtensionMethod { public static class TestExtensionMethod { //extnsion methods must be defined in non generic static class. public static int WordCount(this string v) { return v.Length; }   } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using MyExtensionMethod; namespace CSharpNew { public static class TestExtensionConsumer { public static void TestExtension() { string s = "TEST"; Console.WriteLine(s.WordCount()); Console.ReadLine(); } } }================ ==程式10 =========== using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace FirstLinq { public class ExtensionMethodAndGenerics { private static void GenericTypeResovlerTest1() { GenericTypeResolverTest v = new GenericTypeResolverTest(); v.Value = "TEST2"; v.Test(); } } //generic implicit type resolver. public class GenericTypeResolverTest { public string Value { get; set; } public override string ToString() { return Value.ToString(); } } public static class GenericTypeResolverMethodTest { public static void Test(this T obj) { Console.WriteLine(obj.ToString()); } } }================ ==程式11=========== Test()================ ==程式12 =========== using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace FirstLinq { public class ExtensionMethodAndGenerics { private static void GenericTypeResovlerTest1() { GenericTypeResolverTest[] v2 = new GenericTypeResolverTest[]{ new GenericTypeResolverTest(), new GenericTypeResolverTest()}; v2.Test2(); } } //generic implicit type resolver. public class GenericTypeResolverTest { public string Value { get; set; } public override string ToString() { return Value.ToString(); } } public static class GenericTypeResolverMethodTest { public static void Test2(this IEnumerable obj) { Console.WriteLine(obj.ToString()); } } }================ ==程式13 =========== void Test2 (IEnumerable obj) void Test2(IEnumerable obj)================ ==程式14 =========== IEnumerable p = new string[] { "1111", "2222", "3333" }.Where(delegate (string o) { return o == "2222"; });================ ==程式15 =========== var p = new string[] { "1111", "2222", "3333" }.Where(l => l == "2222"); ================ ==程式16 =========== ………… namespace CSharpNew { public class TestLamba { public delegate int Sum(int x, int y); public void Test2() { //lamba expression can be more complex. Sum sFunc = (x, y) => { var ret = x + y; DateTime d = DateTime.Now; Console.WriteLine("sum time is {0}",d.ToShortDateString()); return ret; }; Console.WriteLine(sFunc(15, 20)); Console.ReadLine(); } } }================ ====程式17 =========== var p1 = new[]{new {Name = "code6421", Address = "Taipen", Title = "Manager"}, new {Name = "tom", Address = "Taipen", Title = "Manager"}, new {Name = "jeffray", Address = "NY", Title = "Programmer"}}; ================ ==程式18 =========== using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FirstLinqHack { class TestHacking { public static void HackLinq() { Persons list = new Persons(); list.Add(new Person { Name = "Code6421", Age = 18, Address = "Taipen" }); var p1 = from o in list select o; Console.WriteLine(p1[0].Name); } } public sealed class Person { public string Name { get; set; } public int Age { get; set; } public string Address { get; set; } } public static class PersonsExtension { public static Persons Select(this Persons source,                    Func selector) { return null; } } public class Persons { private List _list = new List(); public T this[int index] { get { return _list[index]; } } public void Add(T item) { _list.Add(item); } } } static void Main(string[] args) { FirstLinqHack.TestHacking.HackLinq(); }================