Linq learning summary 1-refer to the detailed description of Linq technology, 1-linq

Source: Internet
Author: User

Linq learning summary 1-refer to the detailed description of Linq technology, 1-linq

Two key points:

1. The set of linq operations must implement the IEnumerable interface. Therefore, before 3.0, the set that implements this interface must be converted to a Linq-enabled set through the Cast or TypeOf method;

2. the query expression and the Lame expression can be used together. Which one is convenient to use? They only query the expression for the first time;

 

List <Employee> ils = new List <Employee> () {new Employee () {IDCode = "jack5", Age = 20, littleName = "AB"}, new Employee () {IDCode = "mike444", Age = 12, littleName = "aa"}, new Employee () {IDCode = "mary5", Age = 12, littleName = "zs "}, new Employee () {IDCode = "sm5555", Age = 67, littleName = "yb"}, new Employee () {IDCode = "som", Age = 67, littleName = "cr" }}; ArrayList als = new ArrayList () {new Department () {Name = "jack", DepName = "Foxconn"}, new Department () {Name = "jack", DepName = "translated as"}, new Department () {Name = "mary", DepName = "updated employee"}, new Department () {Name = "sum", DepName = ""}, new Department () {Name = "soom", DepName = ""}}; # chapter 3 of the region query sentence, using the nested grouping solution in the linq technique // The group by clause with Into var va = from c in ils group c by new {c. littleName, c. age} into g select new {Name = g. key, ageC = g. count ()}; var va1 = ils. groupBy (p => new {p. littleName, p. age }). select (p => new {name = p. key, agec = p. count ()}); // The variable type var varT = from c in ils join Department d in als on c. IDCode equals d. name select new {age = c. age, depName = d. depName}; var varT1 = ils. join (als. cast <Department> (), c => c. IDCode, p => p. name, (c, p) => new {age = c. age, depName = p. depName}); // join an alias var varJoin = from c in ils join Department d in als on c. IDCode equals d. name into ao select new {c. IDCode, sum = ao. sum (p => p. depName. length)}; var varJoin1 = ils. groupJoin (als. cast <Department> (), a =>. IDCode, B => B. name, (B, a) => new {B. IDCode, sum =. count ()}); // Let and Where statement var varLet = from c in ils let names = c. IDCode + ":" + c. littleName where names. length> 5 select new {c. age, names}; var varLet1 = ils. select (a => new {a, names =. IDCode + ":" +. littleName }). where (p => p. names. length> 5 ). select (B => new {B. a. age, B. names}); // Generator sentence (multiple from), orderby sentence var varSelMany = from a in ils from B in als. cast <Department> () orderby. age,. department descending select new {. IDCode,. littleName,. age, B. depName}; var varSelMany1 = ils. selectals (p => als. cast <Department> (). select (a => new {p. age,. depName })). orderByDescending (a =>. age ). thenByDescending (a =>. depName); // group by var varGroup = from p in ils group p by p. age into a select. key + ":" + als. capacity; # endregion # solution of region extension operator // always returns ArgumentNullException // select, where has two prototypes, another prototype has index indexes var varWhere = ils. where (p, I) => I <2); // The partition operator take var varTake = ils. take (2); // TakeWhile will jump out of var varTakeWhile = ils as long as the condition does not match. takeWhile (p, q) => p. IDCode. length> 4); // The skip and take parameters var varSkip = ils. skip (2); // The skipwhile and takewhile parameters var varSkipWhile = ils. skipWhile (a, I) =>. IDCode. length> 5 & I <3); // string signature operator var varConcat = ils. take (2 ). concat (ils. skip (2); // concat can be a string of only two sequences. When a string contains multiple sequences, selectsequence can be used; var varSelectMany1 = new [] {ils. take (1), ils. skip (1 )}. selectsort (s => s); // sorting operation. The second prototype can be used to add the number of neighbors. For secondary sorting, thenby and orderbydesding are similar to var varOrderby = ils. orderBy (p => p. IDCode. length); // reverse sequence of reverse sort out // Join and JoinGroup p119 IEnumerable <IGrouping <string, Employee> items = ils. groupBy (p => p. littleName); IEnumerable <IGrouping <string, Department> items1 = ils. groupBy (p => p. IDCode, q => q. department); // set operator distinct, union (union region is different from Concat), intersect (sequence of re-Attention elements after concatenation ), partition T (excluding the elements in the Partition Number and their own duplicates) var ca = ils. distinct (); List <Employee> ils1 = ils. take (2 ). toList <Employee> (); ils1.Add (new Employee () {IDCode = "I added", Age = 33, littleName = "xixi "}); foreach (var v in ils. failed T (ils1) {Console. writeLine (v. littleName);} // element operator var ilsDefaultIfEmpty = ils. where (p => p. IDCode = "hehe "). defaultIfEmpty (). first (); var ilsDefaultIfEmpty1 = ils. where (p => p. IDCode = "hehe "). defaultIfEmpty (new Employee () {IDCode = "heheid "}). first (); // generate the operator Enumerable random between methods Range, Repeat, IEnumerable <int> EnumRange = Enumerable. range (2, 20); foreach (int I in EnumRange) {Console. writeLine (I);} // p145 string str = string. empty; // cast, ofType, AsEnumerable (

  

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.