C # the syntax of LiNq and common extension methods,
First, let's talk about it. This blog post was written for the first time, mainly for some of my excerpts, and I hope it will help you.
Basics of Linq
• LINQ (pronunciation link): Linq To SQL (obsolete), Linq To Object, Linq To XML, and Linq To entity. Objective: To operate data in a unified manner. It looks like an SQL statement, but it has nothing to do with SQL. • Var keyword. The var type is used to simplify the declaration of the type. var I = 2 does not mean that I is non-typed. The Compiler automatically deduce the value on the right (this is called type inference) the value represented by var. Var can only be used in statements and cannot be used in return values or parameter lists. • The anonymous type, var s = new {Name = "jim", Sex = M}, can be referenced using the s. Name method in the subsequent code. The anonymous type is not a dynamic type and is eventually compiled into a class, which can be viewed by Reflector. If the compiler encounters an anonymous type with identical attributes, it will reuse this type instead of creating a new type each time. It is also a manifestation of type inference. • String name = "ljw"; int age = 30; var p = new {name, age, name. Length}; default name attribute. • If the inferred attribute name conflicts, you must explicitly specify var p = new {name. length, lengh2 = "C #". length} • if you cannot call the Linq method when writing a program, check whether the system is using.. 1 Table1TableAdapter adapter = new Table1TableAdapter (); 2/* 3 strong DataSet and Var. database1DataSet. table1DataTable dt = adapter. getData (); 4. Strong DataSet and Var. database1DataSet. table1Row row = dt [0]; 5 */6 var dt = adapter. getData (); 7 var row = dt [0]; 8 var aa = row. aa;View Code
Sort and group
Orderby person. Age descending
1 int [] values = {1, 2, 5, 2, 3, 5, 5, 3, 4, 3 }; 2 var result = from I in values3 // sort by I, g indicates grouping 4 group I by I into g5 select g. key;View Code
Extension Method
The following methods are extensions of IEnumerable <T>:
Average calculates the Average value, Min Max Sum Count Min;
Concat connects two sequences; // equivalent to SQL's Unoin all
Indicates whether the Contains sequence Contains the specified element;
Distinct obtains non-repeating elements in the sequence;
Using t to obtain the difference set of two sequences;
Intersect obtains the intersection of the two sequences;
First obtains the First element of the sequence;
Single gets the unique element of the sequence. If the number of elements is not one, an error is returned;
FirstOrDefault gets the first element of the sequence. If no element exists, the default value is returned;
Linq can only be used for Fan-type sequences, IEnumerable <T>. For non-Fan-type sequences, Cast or OfType can be used.
IEnumerable method:
Cast <TResult>: As Linq needs to perform fan-type operations, the Cast method can be used to convert non-Fan-type IEnumerable sequences such as the. Net class in earlier versions to fan-type sequences. ArrayList l; IEnumerable <int> il = l. Cast <int> ();
OfType <TResult>: Cast attempts to convert all elements in the sequence to the TResult type. If the non-fan sequence to be converted contains other types, an error is returned. OfType converts only elements of the specified type in the sequence to the fan sequence.