Anonymous delegates in C # and examples of lambda expressions

Source: Internet
Author: User
I. C # from 1.0 to 4.0, with the support of LINQ, Generics, the code is getting simpler and more elegant

1 int[] Nums = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; 2             ienumerable<int> newnums = from n in nums where n > 0 select N; 3             newnums = newnums.where (New func<i Nt,int, bool> (delegate (int I,int index) {return i < Index;})); 4             newnums = Newnums.where (new func<int, int, bool> ((int i, int index) = = < index)); 5             newnums = Newnu Ms. Where (delegate (int i, int index) {return i < index;}); 6             newnums = Newnums.where ((i, index) = = < index), 7             foreach (var i in newnums) 8             {9                 Console.Write Line (i);             

Two. Collection operation, also suitable for EF database operation

1. Creation of two entity classes

1 Public     class Store 2     {3 public         string Id, 4 public         string Name, 5     } 6 public class person     7
  
   {8 Public         string name {get, set;} 9 public         int Age {get; set;} Ten public         string StoreId {get; set;} One     }
  

2. Inserting data

1             var Stores = new list<store> () 2             {3                 new store () {id= "1", name= "1 Class"}, 4                 new Store () {id= "2", name= "Class 2"} 5             }; 6  7             var Persons = new list<person> () 8             {9                 new person () {name= "P1", Age=1, storeid= "1"},10                 New Person () {name= ' P2 ', age=2, storeid= ' 1 '},11                 new Person () {name= ' P3 ', age=3, storeid= ' 1 '},12                 new person () { Name= "P4", Age=4, storeid= "2"},13                 new Person () {name= ' P5 ', age=5, storeid= ' 1 '},14                 new Person () {name= ' P6 ', Age=6, storeid= "2"},15                 new Person () {name= ' P7 ', age=7, storeid= ' 1 '},16                 new Person () {name= ' P8 ', age=8, Storeid= "1"}17             };

3. Check the number of people younger than 3 years of age 1 and 2 classes respectively

1             var plst = persons.where (o = o.age > 3). GroupBy (o = O.storeid). Select (g = new {StoreId = G.key, Count = G.count ()}). Join (Stores, S = = S.storeid, p = = p.id, (s, p) = = new {S.storeid, storeName = P.name, s.count}); 2             foreach ( var p in Plst) 3             {4                 Console.WriteLine (P.storename + "with" + P.count + "personal"); 5             }

4. Output

There are 2 people in Class 2 and 3 in Class 1.
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.