C # Knowledge System (I) --- commonly used LInq and lambda expressions

Source: Internet
Author: User

C # Knowledge System (I) --- commonly used LInq and lambda expressions
LinQ is one of our common technologies. We need to make a series of adjustments to the data, such as sorting, conditional filtering, summation, grouping, and multi-table join. Lambda is our commonly used syntax sugar. It works seamlessly with linq and is used without knowing it. It mainly looks at the concise code and makes the code too high... Next, let's take a look at common basic data first.

// User class public class User {[DataMember] public int ID {get; set;} [DataMember] public string Name {get; set ;} [DataMember] public string Address {get; set;} [DataMember] public string Phone {get; set;} [DataMember] public string Sex {get; set ;} [DataMember] public int Age {get; set;} [DataMember] public int SchID {get; set ;}} /// <summary> /// School class /// </summary> internal class School {public int SchID {get; set;} public string SchName {get; set ;} public School () {} public School (int id, string name) {SchID = id; SchName = name ;}} /// <summary> /// Company class /// </summary> internal class Company {public string Name {get; set;} public List <User> Users {get; set ;}public Company () {}public Company (string name, List <User> list) {Name = name; Users = list ;}}

 

Next, initialize the test data.
User user1 = new User () {ID = 111, Name = "Zhang Yi", Address = "XX District, Shanghai", Phone = "13547878787", Age = 30, sex = "male", SchID = 1}; User user2 = new User () {ID = 112, Name = "", Address = "XX District, Shanghai ", phone = "13547878783", Age = 30, Sex = "female", SchID = 1}; User user3 = new User () {ID = 113, Name = "James ", address = "XX District, Shanghai", Phone = "13547878784", Age = 30, Sex = "male", SchID = 1}; User user4 = new User () {ID = 114, Name = "", Address = "XX District, Shanghai", Phone = "13547878785", Age = 30, Sex = "male", SchID = 1 }; list <User> userlist = new List <User> (4) {user1, user2, user3, user4}; List <School> Schlist = new List <School> () {new School (1, "Wuhan University"), new School (2, "Huazhong University of Science and Technology"), new School (3, "Huazhong Normal University ")};

 

Sum
// Sum var sum = userlist. where (a => {return. ID> 0 ;}). sum (a =>. ID); Console. writeLine (sum); // another write method: sum = (from a in userlist where. ID> 0 select. ID ). sum (); Console. writeLine (sum); IEnumerable. the number of parameters in the Where () method. The delegate type Func <TSource, bool> is preceded by the parameter and the last one is the return value.

 

Max Min/max var Max = userlist. max (a =>. ID); Console. writeLine (max); // minimum value var min = userlist. min (a =>. ID); Console. writeLine (min); loop ForEach
// Cyclically output userlist. ForEach (a ==>{ if (a. Age> 20) {Console. WriteLine (a. ID );}});

 

Conditional filter where // filter var user = userlist. where (a =>. ID = 114 ). single (); // filter all male users var templist = userlist. where (a =>. sex = "male "). toList (); sort // sort by ID in reverse order templist = userlist. orderByDescending (a =>. ID ). toList (); // ascending templist = userlist. orderBy (a =>. ID ). toList (); group ToLookup and GroupBy
// Group var lookup = userlist. toLookup (a =>. sex); foreach (var item in lookup) {Console. writeLine (item. key); foreach (var sub in item) {Console. writeLine ("\ t" + sub. name + "" + sub. age) ;}} // another var dic = userlist. groupBy (a =>. sex); foreach (var item in dic) {Console. writeLine (item. key); foreach (var sub in item) {Console. writeLine ("\ t" + sub. name + "" + sub. age );}}

 

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.