Linq processes List data, and linq processes list data.
Overview:It is also called Language Integrated Query. It is a query language similar to SQL. Language integration query is available to developers. NET Programming Language (such as C #) to query the data source, the main data source is the collection object in the memory, ADO.. NET datasets, databases, and XML. developers can easily query heterogeneous data sources without learning many query syntaxes in different categories.
Three forms:Composite query, Lambda expression, and hybrid query.
1. The methods for querying set data are as follows:
Compound query:
Var result = from n in dataList orderby n. TimeWritten descending select n; // you can view n as a dataList. The result is that all values in dataList are sorted in descending order of the time field.
Lambda expression query:
Var result = dataList. Select (p => new {p. ID, p. Name, p. TimeWritten}); // Lambda expressions evolve from anonymous methods. In this Code, p is a parameter and multiple parameters can be expressed as (p, m. "=>" Is the release symbol, followed by the return value. The return value of this example is the field specified in the object.
Hybrid query:
Var result = from n in dataList select n. OrderByDescending (p => p. TimeWritten); // query all data in the Set in descending order of the time field. The Ascending Order is OrderBy ().
2. delete data
List <NewSingle> dataList = new List <NewSingle> ();
NewSingle single = dataList. Single (p => p. ID = 1 );
DataList. DeleteOnSubmit (NewSingle );
DataList. SubmitChanges ();
3. insert data
NewSingle single = new NewSingle ();
Single. Name = "Test1 ";
Single. TimeWritten = this. DateTime. Now. ToString ();
Single. Subject = "subject ";
DataList. InsertOnSubmit (single );
DataList. SubmitChanges ();
4. modify data
NewSingle single = from n in dataList where n. ID = 3 select n;
Single. Name = "Test2 ";
Single. TimeWritten = "2011-3-17 ";
Single. Subject = "subject2 ";
DataList. SubmitChanges ();
Note: Convert conversion is not supported in linq.
5. query the bound DropDownList
Var data = from n in dataList select new {Name = n. Name, ID = n. ID };
DropDownList1.DataTextField = "Name ";
DropDownList1.DataValueField = "ID ";
DropDownList1.DataSource = data;
DropDownList1.DataBind ();
6. Bind The GridView
GridView1.DataSource = dataList. Select (p => p );
GridView1.DataBind ();
7. Table join
Var result = from a in mytest. stuInfo join B in mytest. stuMarks on. stuNo equals B. stuNo (into c) select new {Name =. stuname, B. writtenExam };
8. Other Methods
DeleteAllOnSubmit <T> (object) deletes a T-type object.
Where () condition query returns bool
GroupBy () Group
SubString (n, m) captures m pieces of data starting from n
Skip (number) skips a specified number of items.
Take (number) extracts a specified number of items]
TakeWhile () is extracted based on specified conditions
SkipWhile () is extracted from the first item that does not conform to the small part according to the specified condition hop option.
ThenBy (s => s. Name) composite sorting, which appears in the Multi-field list and is placed behind OrderBy
Distinct () filters duplicate data
ToList () to List
Count () total data
Max ()/Min () Maximum/minimum
Average () Average
Sum ()
Range () Example: num = Enumerable. Range () generates 10 data records starting from 1.
Repeat (repeated value, repeated times) to generate repeated items
Transport: http://blog.163.com/lm_regina/blog/static/17419653620110287530239