From basics to projects

Source: Internet
Author: User

From basics to projects
The following is a book note in C # graphic tutorial

LINQ is
Directly embed C # into a strong-type Query LanguageFeatures related to LINQ:Implicit local variables: use var object/set initialization syntax: object initialization statement/object initializer/object initialization table Lamdba expression: lambda expression Extension Method // write a static class to add a method to a known class. It seems that this method is already in this class. anonymous type: anonymous typeThe following table describes the applicability of LiNQ: LINQ syntaxThere are two formal syntaxes, which have no performance difference: for example, if an array numbers method syntax has been defined: var A = numbers. where (x => x <20); // The Lambda query syntax is used here: var A = from n in numbers where n <20 select n; // an object is returned here, which can be used to describe the specific query steps. No query action query variable is provided here: the query result is generally a set of enumerated data or an object calledScalarThe preceding query syntax does not contain query results. On the contrary, the compiler creates A code scalar that can execute the query: int count = (from n in numbers where n <20 select n ). count (); // here is the combination of a query statement and a method statement. count contains a real integer, which can only be obtained through a real run query. Query expression StructureNote:

  • Appears in a certain order
  • The from clause and the select... group clause are required.
  • Other statements are optional.
  • The select clause is at the end of the expression.
  From clauseThe from clause specifies the data set to be queried. The syntax starts with the from clause for each LINQ query: Frome type Item in Items // type is optional, because the compiler will help you infer the difference with foreach, foreach specifies from the first to the end, the from clause specifies that each item of the set must access foreach and execute the subject when the code is encountered. The from clause only creates the background code object that can execute the query. Jion join:Join is used to join data of multiple sets. Two sets are accepted and a temporary object set is created. Each object contains all fields in the original set. syntax: self-understanding: join is to combine the set before join and the set after jion into a set. Of course, you must set some selection conditions or read the code statement: Sample Code:
  1. 1 // This Code declares two classes. 2 // The first represents the student. Each student has the student's name and ID. 3 // The second represents the course, each course class has a Student ID and Course name 4 class Program 5 {6 public class Student 7 {8 public int StID; 9 public string LastName; 10} 11 public class CourseStudent12 {13 public string CourseName; 14 public int StID; 15} 16 // create the Student class and course class 17 globally // The same ID18 static Student [] students = newStudent [] {19 new Student {StID = 1, lastName = "Carson"}, 20 new Student {StID = 2, LastName = "Klassen"}, 21 new Student {StID = 3, LastName = "Fleming"}, 22 }; 23 static CourseStudent [] studentsInCourses = new CourseStuden t [] {24 new CourseStudent {CourseName = "Art", StID = 1}, 25 new CourseStudent {CourseName = "Art
       ", StID = 2}, 26 new CourseStudent {CourseName =" History ", StID = 1}, 27 new CourseStudent {CourseName =" History ", StID = 3 }, 28 new CourseStudent {CourseName = "Physics", StID = 3}, 29}; 30 static void Main () 31 {32 var query = from s in students // start the query, query 33 join c in studentsInCourses on s in a set. stID equals c. stID34 // and the set is composed of two parts of the set. The preceding meaning is: 35 // define the students iteration variable a36 // define the studentsInCourses iteration variable c37 // join the members of the connected set and generate a new set 38 // The join result is: take out the two sets with the same StID to form a new set 39 where c. courseName = "History" 40 // find the 41 select s Member of CourseName = "History" in the new collection. lastName; 42 // return the LastName of these Members to 43 foreach (var q in query) 44 Console. writeLine ("Student taking History: {0}", q); 45 Console. readKey (); 46} 47}

     

Join process Legend: The left students right CourseName takes students each time the element is more CourseName compare and compare the StID value shadow of the two is the join result From... let... where clauseFrom statement each from statement introduces a new data source
  1. 1 class Program 2 {3 static void Main () 4 {5 var groupA = new [] {3, 4, 5, 6}; 6 var groupB = new [] {6, 7, 8, 9 }; 7 var someInts = from a in groupA // required first from Statement 8 from B in groupB // from statement 9 select new {a, B, sum = a + B}; // create an anonymous 10 foreach (var a in someInts) 11 Console. writeLine (a); // amazing, "a =", "B =" automatically written 12} 13}

     

The let clause accepts an expression-based operation and copies it to an identifier that needs to be used in other operations.
  1.  1 class Program 2 { 3     static void Main() 4     { 5         var groupA =new[]{3,4,5,6}; 6         var groupB =new[]{6,7,8,9}; 7         var someInts =from a in groupA 8                     from b in groupB 9                     let sum = a + b10                     where sum ==1211                     select new{ a, b, sum };12         foreach(var a in someInts )13         Console.WriteLine( a );14     }15 }

     

Result: The where clause removes non-conforming items based on subsequent operations. Note: The query expression can be any one in the from... let... where clause.
  1. 1 static void Main () 2 {3 var groupA = new [] {3, 4, 5
       , 6}; 4 var groupB = new [] {6, 7, 8, 9 }; 5 var someInts = from int a in groupA 6 from int B in groupB 7 let sum = a + B 8 where sum> = 11 9 where a == 410 select new {, b, sum}; 11 foreach (var a in someInts) 12 Console. writeLine (a); 13}

     

OrderbyThe orderby clause accepts an expression and returns the result item expression in sequence based on the expression. The expression is usually a field, but not necessarily a numeric type. It can also be followed by a string expression (ascending or descending) // optional. It is used to set the ascending or descending order. The default values are ascending.
  1. 1 classProgram 2 {3 static void Main () 4 {5 var students = new [] 6 {7 new {LName = "Jones", FName = "Mary", Age = 19, major = "History"}, 8 new {LName = "Smith", FName = "Bob", Age = 20, Major = "CompSci "}, 9 new {LName = "Fleming", FName = "Carol", Age = 21, Major = "History"} 10}; 11 var query = from student in students12 orderby student. age ascending //
       Descending13 select student; 14 foreach (var s in query) 15 {16 Console. writeLine ("{0}, {1 }:{ 2}-{3}", 17 s. LName, s. FName, s. age, s. major); 18} 19 Console. readKey (); 20} 21}

     

  Select... group clauseIt consists of two parts: select... group: select clause and group... by clause select clause specifies which part of the selected object is selected. This part can be: A new object consisting of several fields of a field data item of the entire data item // anonymous type group... bygroup... the by clause is optional and used to specify how the selected items are grouped. group statement bar select objects are grouped according to some standards. If only the from Statement and the goup by statement are written, IEnumerable <IGrouping <key, value> is returned.
  1. 1 classProgram 2 {3 static void Main () 4 {5 var students = new [] 6 {7 new {LName = "Jones", FName = "Mary", Age = 19, major = "History"}, 8 new {LName = "Smith", FName = "Bob", Age = 20, Major = "CompSci "}, 9 new {LName = "Fleming", FName = "Carol", Age = 21, Major = "History"} 10 }; 11 var query = from student in students12 group student by student. major; 13 // query returns IEnumerable <IGrouping <key, value>, so 14 foreach (var s in query) 15 {16 Console cannot be printed directly below. writeLine ("{0}", s. key); // key is the group Key 17 foreach (var t in s) 18 Console. writeLine ("{0}, {1}", t. LName, t. FName); 19} 20} 21}

     


Into clauseThe explain continuation clause can accept a part of the query results and assign a new name to it for other queries.
  1. 1 classProgram 2 {3 static void Main () 4 {5 var groupA = new [] {,}; 6 var groupB = new [] {, 6, 7 }; 7 var someInts = from a in groupA 8 join B in groupB on a equals B 9 into groupAandB10 from c in groupAandB11 // select the same part of groupA and groupB and name this part grupAandB12 select c; 13 foreach (var a in someInts) 14 Console. write ("{0}", a); 15} 16}

     

Linq core assembly





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.