Секреты LINQ

  • View
    1.177

  • Download
    9

Embed Size (px)

Text of Секреты LINQ

. LINQ. 2007-2010 ____________________________________________________________________________________________________________________________

LINQ # C++ . Anders Hejlsberg' ( #), Microsoft .

LINQ, . LINQ C# ( VB), , Intellisense . : LINQ , - . LINQ (Language Integrated Queries) . (, ) , . , LINQ, SQL, XQuery, , . , : C++, C#, Java, PL/I, Ruby, Python . , . , . : A = , R , O . , = , R , O : { +, , / , }. , - , : C = , D , M , .

. (, , ). (, , , ). ( , -) , . , , , -.

, , . , C#, , . LINQ - (Lambda Expressions) (Expression Trees) , . . (Expression), , C#. , -, . LINQ : (), Web-service, XML-, . , LINQ , IEnumerable. LINQ. , LINQ , . IEnumerable : IEnumerator GetEnumerator(), 50- , , extension methods (). Enumerable . :

. LINQ. 2007-2010 ____________________________________________________________________________________________________________________________

All , - - . Cast . Concat (concatenates) . Contains , - . ElementAt . Except , . GroupBy . Intersect , . Join (correlates) . OrderBy . Range(int start, int count) . Select . ToArray . ToDictionary , . ToList generic- . Where . Union .

: T-SQL, PL/SQL . . LINQ-, (LINQ queries) (native) SQL. , LINQ- . , LINQ, LINQ to SQL LINQ TSQL, , , SQL. IQueryable, IEnumerable. LINQ , . . , , . IQueryable . , Person : Student Professor, T Person, Student Professor.

C# LINQ C#, C#, 3.0. (.NET Framework 3.5) LINQ . Visual Studio 2008 , C# . (.Net Framework 1.0) C# 77 . .abstract char do finally in new protected sizeof true using as checked double fixed int null public stackalloc try virtual base class else float interface object readonly static typeof volatile bool const enum for internal operator ref string uint void break continue event foreach is out return struct ulong while byte decimal explicit goto lock override sbyte switch unchecked case default extern if long params sealed this unsafe catch delegate false implicit namespace private short throw ushort

, ++ 44 . ( .Net Framework 3.5), 77 , C# 24 (Contextual Keywords). .add from join ascending get let by global on descending group orderby equals into partial (type)

. LINQ. 2007-2010 ____________________________________________________________________________________________________________________________ partial (method) var remove where (generic type constraint) select where (query clause) set yield value

, . , where (generic) , LINQ ( ) . ? Nullable, (, decimal?), C# LINQ, . LINQ- Nullable- , , . .int? i = null;

C# ( ). null (value) , . . , . , ?? ( ). null. , ??.string s = null, test = s ?? "String s == null"; Console.WriteLine (test); s = "Set of C# Keywords"; test = s ?? "String s == null"; Console.WriteLine (test);

C# , LINQ, C#, . , . (local variable type inference). : var s = "Hi"; string s = "Hi"; (auto-implemented properties). c (, ) :decimal salary; public decimal Salary { get { return salary; } set { salary = value; } }

.public decimal Salary { get; set; }

. (object initializers). , :public class Person { public string Name { get; set; } public DateTime Birth { get; set; } public decimal Salary { get; set; } }

Person, :Person alex = new Person(){ Name = "Alex", Birth = new DateTime(1994, 2, 26), Salary = 10000m };

, Person . .

. LINQ. 2007-2010 ____________________________________________________________________________________________________________________________

Generic-, generic- generic-. generic . , generic- , . (template), ++, generic. . -, . . , . .NET (LINQ). ASP.NET AJAX (Asynchronous JavaScript and XML). Web- () . , , JavaScript.

. , , , , . :var s = "This is a string of characters"; var a = new[] { 1, 2, 3, 4 };

s a . string, int[]. new[]. , ( managed heap). ( C# 2.0) : int[] a = new int[] { 1, 2, 3, 4 };. var.CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures); foreach (var v in cultures) Console.WriteLine("{0,52} {1}", v.EnglishName, v.IetfLanguageTag);

v CultureInfo. :InstalledFontCollection collection = new InstalledFontCollection(); foreach (var font in collection.Families) Console.WriteLine(font.Name);

font FontFamily. . MSDN, , , Google. , MSDN . , :public string Name { get { return name; } set { name = value; } }

, ( name ), , . , DataBinding . , nonsense. , . , , , .class Stud { public string Name { get; set; } public List Marks { get; set; } }

. LINQ. 2007-2010 ____________________________________________________________________________________________________________________________

. get set . LINQ, MSDN, . : " ?" : Asymmetric Accessor Accessibility. :public string Name { get; private set; } public string Name { get; internal set; }

, Name , , . , . (internal set) , Name , () Assembly. () . default- .Person alex = new Person(){ Name = "Alex", Birth = new DateTime(1994, 2, 26), Salary = 10000m };

, :var persons = new[] { new Person(){ Name = "Bale", Birth = new DateTime(1981, 3, 26), Salary = 10000m }, new Person(){ Name = "Dale", Birth = new DateTime(1974, 1, 11), Salary = 15000m }, new Person(){ Name = "Gail", Birth = new DateTime(1964, 6, 12), Salary = 20000m } };

, . (infered) . , ( C#). -, Person . :Person[] persons = { new Person("Alex", new DateTime(1981, 3, 26), 10000m), . . . }

:Person persons; persons = new Person[] { new Person("Alex", new DateTime(1981, 3, 26), 10000m), . . . }

new . : new[] {. . .}. , generic- :var persons = new List { new Person(){ Name = "Bale", Birth = new DateTime(1981, 3, 26), Salary = 10000m }, . . . }

Generic-

. ( - ). , delegate, ( ). ( ), . , . , : .

. LINQ. 2007-2010 ____________________________________________________________________________________________________________________________

System ( System.Core) : Action Func, . Action. :public delegate void Action(); // System.Core.dll

void() Action. . Action.class Program { static void Inform() { Console.WriteLine(Environment.MachineName); } static void Say() { Console.WriteLine("I am an Action delegate"); } static void Main() { Action a = Inform; a(); a += Say; a(); } }

Action , System.Core. a Action Inform. , Inform Action. a Action. : a(); Inform();. a += Say; a Say. a(); : Inform Say. , (InvocationList) ( ), : Inform Say. Action ( ).

( ) :class TestDelegate { public void Test() { Action a = new Action(Say); a += new Action(Do); Delegate[] dd = a.GetInvocationList(); Console.WriteLine("Testing Delegate's InvocationList\n"); int i = 0; foreach (var d in dd) Console.WriteLine("{0}. Method = {1}, CallingConvention = {2}, Target = {3}, Returns = {4}", (++i).ToString(), d.Method.Name, d.Method.CallingConvention, d.Target, d.Method.ReturnType.Name); } void Say() { Console.WriteLine("Saying"); } void Do() { Console.WriteLine("Doing"); } } class Program { static void Main() { new TestDelegate().Test(); } }

, .Testing Delegate's InvocationList 1. Method = Say, CallingConvention = Standard, HasThis, Target = Test.TestDelegate, Returns = Void 2. Method = Do, CallingConvention = Standard, HasThis, Target = Test.TestDelegate, Returns = Void

System Action, (, generic). , , () .public delegate void Action (T obj); // System.Core.dll

Action , C++. delegate template. .. generic, (. http://www.intuit.ru/department/pl/csharp/). , generic- . Action T. , Action

. LINQ. 2007-2010 ____________________________________________________________________________________________________________________________

void (int), Action void (Person). . Program Inform.static void Inform(string name) // { Console.WriteLine(Environment.ExpandEnvironmentVariables(name)); }

Main Inform generic- Action .Action s = Inform; s("%SystemDrive%");

Action s void (string). : s ("%SystemDrive%"); Inform ("%SystemDrive%");.

Action , .public delegate void Action (T1 arg1, T2 arg2);

T1 T2. . Program SaveImage, Action.static void SaveImage(string file, byte[] data) { File.WriteAllBytes(file, data); }

Main SaveImage generic- Action.byte[] img = File.ReadAllBytes(@"C:\Windows\Web\Wallpaper\Landscapes\img11.jpg"); Action save = SaveImage; save(@"C:\Test.jpg", img); // C:\Test.jpg

System.Core.dll Action. void- . , , . Action void, 0