[Linq] common syntax summary and linq Summary
Language inheritance Query(Language Integrated Query, LINQ). in C # programming Language, the Query syntax is Integrated, and different data sources can be accessed using the same syntax. LINQ provides an abstraction layer for different data sources, so you can use the same syntax.
Public class Book {public int Id {get; set ;}/// <summary >/// title /// </summary> public string BookName {get; set ;} /// <summary> /// Author id /// </summary> public int AutherId {get; set ;} /// <summary> /// Type /// </summary> public string Type {get; set ;} /// <summary> /// Price /// </summary> public decimal Price {get; set ;} /// <summary> /// Sales // </summary> public int Sales {get; set ;}} public class Auther {public int Id {get; set ;} /// <summary> /// author /// </summary> public string AutherName {get; set ;}} public class Library {public string Address {get; set ;} /// <summary> // Book // </summary> public List <Book> BookList {get; set ;}}
1. Conditional query (Where)
Var query = from book in bookList where book. price> 50 orderby book. sales descending, book. bookName select book; // equivalent to var query = bookList. where (n => n. price> 50 ). orderByDescending (g => g. sales ). thenBy (y => y. bookName );
Note that,Latency Query
var ary = new List<string>(){ "Aa", "Bb", "Cc"};var a1 = ary.Where(n => n.Contains("a"));//["Aa"]ary.Add("Ga");a1;//["Aa", "Ga"]
To solve this problem, you only need to use ToList ();
var ary = new List<string>(){ "Aa", "Bb", "Cc"};var a1 = ary.Where(n => n.Contains("a")).ToList();//["Aa"]ary.Add("Ga");a1;//["Aa"]
Index Filtering
// The Sales volume is greater than 50 and it is an odd line var query = bookList. Where (n, index) => n. Sales> 50 & index % 2! = 0 );
2. Composite query (Selectparameters)
// Search for novels in all libraries. var query = from library in libraryList from book in library. bookList where book. type = "novel" select book; // equivalent to query = libraryList. selectcenters (n => n. bookList ). where (g => g. type = "novel"); // organizes the returned results query = libraryList. selectcenters (n => n. bookList, (n, g) => new {n. address, g. bookName, g. sales }). where (y => y. sales> 100 );
3. Set connection
// Inner join var query1 = from book in bookList join auther in autherList on book. autherId equals auther. id select new {book. bookName, auther. autherName}; // The group connection var query2 = from auther in autherList join book in bookList on auther. id equals book. autherId into items select new {auther. autherName, Books = items}; // left Outer join var query3 = from book in bookList join auther in autherList on book. autherId equals aut Her. Id into items from auther in items. DefaultIfEmpty () select new {book. BookName, AutherName = auther = default (Auther )? "None": auther. autherName}; // multi-condition join var query4 = from book in bookList join auther in autherList on new {name = book. bookName, id = book. autherId} equals new {name = auther. autherName, id = auther. id} select book;
4. Sorting (OrderBy and ThenBy)
Var query = from book in bookList orderby book. sales descending, book. autherId, book. price descending select book; // equivalent to query = bookList. orderByDescending (n => n. sales ). thenBy (g => g. autherId ). thenByDescending (y => y. price );
5. Group)
// Single condition group var query = from book in bookList group book by book. type into bs select bs. first (); // equivalent to query = bookList. groupBy (n => n. type ). select (g => g. first (); // multi-condition grouping var query = from book in bookList group book by new {book. type, book. autherId} into bs select new {Type = bs. first (). type, AutherId = bs. first (). autherId, Count = bs. count ()}; // equivalent to query = bookList. groupBy (n => new {n. type, n. autherId }). select (g => new {Type = g. first (). type, AutherId = g. first (). autherId, Count = g. count ()});
6. Merge and partition (Zip, Take, and Skip)
Int [] numbers = {1, 2, 3}; string [] words = {"One", "Two", "Three", "Four "}; // The elements are combined in sequence, and the length is a small set of IEnumerable <string> zip = numbers. zip (words, (n, g) => n + "=" + g); // ["1 = One", "2 = Two ", "3 = Three"] // skip the first n elements of the Set var skip = words. skip (3); // ["Four"] // gets the first n elements of the set, with a delay var take = numbers. take (2); // [1, 2] int pageSize = 3; // the size of each page int pageNum = 0; // The number of pages var page = words. skip (pageSize * pageNum ). take (pageSize); // ["One", "Two", "Three"]
7. Set Operations (Distinct, Union, Concat, Intersect, and Except T)
Int [] ary1 = {1, 2, 2, 4, 5}; int [] ary2 = {3, 5, 5, 6, 10, 7}; // merge, automatic deduplication var union = ary1.Union (ary2); // 1, 2, 3, 4, 5, 6, 7, 10 // merge, var concat = ary1.Concat (ary2); // 1, 2, 2, 4, 5, 3, 5, 5, 6, 10, 7 // de-duplicate var distict = ary1.Distinct (); // 1, 2, 4, 5 // take the intersection and automatically remove var intersect = ary1.Intersect (ary2 ); // 5 // obtain the complementary set and automatically remove the var distinct T = ary1.t T (ary2); // 1, 2, 4
8. type filtering (ofType)
object[] data = { "one", 1 , 2 ,"three"};var query = data.ofType<string>();
9. Aggregate operators (Count, Sum, Min, Max, Average, and Aggregate)
// Number of var count = bookList. count (n => n. sales> 50); // sum var sum = bookList. sum (n => n. price); // the minimum value var min = bookList. min (n => n. sales); // maximum var max = bookList. max (n => n. price); // average value var average = bookList. average (n => n. sales); // accumulate, total Sales var aggregate1 = bookList. select (n => n. sales ). aggregate (g, y) => g + y); // accumulate, initial value var aggregate2 = bookList. select (n => n. sales ). aggregate (10, (g, y) => g + y); // accumulate, initial value, result processing var aggregate3 = bookList. select (n => n. sales ). aggregate (10, (g, y) => g + y, result => result/100 );
10. Conversion operators (ToArray, ToDictionary, ToList, ToLookup, and Cast)
Book [] ary = bookList. toArray (); List <Book> list = bookList. toList (); Dictionary <int, Book> dic = bookList. toDictionary <Book, int> (n => n. id); // convert to a LookUp set. key-ILookup, an internal set of key groups, <string, Book> look = bookList. toLookup (n => n. type); IEnumerable <Book> cast = ary. cast <Book> ();