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
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 = "failed "},
New Department () {Name = "mary", DepName = "too many "},
New Department () {Name = "sum", DepName = "mobile "},
New Department () {Name = "soom", DepName = "Tongtong "}
};
 
# Chapter 3 of "region lookup into sentence": solution to "linq "
// Group by clause with
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 ()});
 
 
 
// Volume 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 sentence
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 clause
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 = a. IDCode + ":" + a. littleName })
. Where (p => p. names. Length> 5)
. Select (B => new {B. a. Age, B. names });
 
 
// Generator sentence (multiple from) and orderby sentence
Var varSelMany = from a in ils
From B in als. Cast <Department> ()
Orderby a. Age, a. Department descending
Select new {a. IDCode, a. littleName, a. Age, B. DepName };
Var varSelMany1 = ils. selectals (p => als. cast <Department> (). select (a => new {p. age,. depName })). orderByDescending (a =>. age ). thenByDescending (a =>. depName );
 
 
// Group
Var varGroup = from p in ils
Group p by p. Age
Into
Select a. Key + ":" + als. Capacity;

# Endregion
 
# Region extension operator Decoding
// Always returns ArgumentNullException.
// Select, where both have two prototypes, and the other has index data.
Var varWhere = ils. Where (p, I) => I <2 );
 
// Take Operator
Var varTake = ils. Take (2 );
 
// TakeWhile will jump out as long as the condition does not match
Var varTakeWhile = ils. TakeWhile (p, q) => p. IDCode. Length> 4 );
 
// Skip and take mutual effect
Var varSkip = ils. Skip (2 );
 
// Skipwhile and takewhile interact with each other
Var varSkipWhile = ils. SkipWhile (a, I) => a. IDCode. Length> 5 & I <3 );
 
// String 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, SelectMany can be used;
Var varSelectMany1 = new [] {ils. Take (1), ils. Skip (1)}. SelectMany (s => s );
 
// Sorting operation. The second prototype can be added to the histogram. thenby and orderbydesding are used for secondary sorting.
Var varOrderby = ils. OrderBy (p => p. IDCode. Length );
 
// Reverse sequence of reverse
// 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 ), distinct t)
Var ca = ils. Distinct ();
List <Employee> ils1 = ils. Take (2). ToList <Employee> ();
Ils1.Add (new Employee () {IDCode = "", Age = 33, littleName = "xixi "});
Foreach (var v in ils. Grouping 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 Enumerable aggregate Operator Method 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 () [used To list the sequence program columns, and aggregate is used for Linq To SQL]

 
# Endregion

This article is organized and published by anthropomorphic www.zaojuzi.com.


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.