码迷,mamicode.com
首页 > Windows程序 > 详细

C# LINQ

时间:2016-10-26 09:24:55      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:order   desc   span   null   exce   color   log   union   custom   

Sorting: 

  • OrderBy
  • ThenBy
  • OrderByDescending
  • ThenByDescending
  • Reverse
1 public IEnumerable<Customer> SortByName(List<Customer> cumtomerList)
2 {
3     return customerList.OrderBy(c => c.LastName)
4         .ThenBy(c => c.FirstName);
5 }

OrderBy a property that can have null value. null value will be sorted at the first.

 

Creating:

  • Range
  • Random 
  • Repeat
 1 public IEnumerable<int> BuildIntegerSequence()
 2 {
 3     var integers = Enumerable.Range(0,10)         //0,1,2,3,4,5,6,7,8,9
 4                     .Select(i => 5+(10*i));        //5, 15 ...
 5 
 6     return integers;
 7 }
 8 
 9 public IEnumerable<string> BuildStringSequence()
10 {
11 
12     var strings = Enumerable.Range(0,10)
13                     .Select(i => ((char)(A+i)).ToString());  //A,B,C,D,E,F,G,H,I
14 
15     Random rand = new Random();
16     var randomStrings = Enumerable.Range(0,10)    
17                     .Select(i => ((char)(A+rand.Next(0,26))).ToString());
18 
19     return strings;                
20 }

 

Comparing/Combining:

  • Intersect
  • Except
  • Concat
  • Distinct
  • Union
 1 public IEnumerable<int> CompareSequence()
 2 {
 3     var seq1 = Enumerable.Range(0,10);
 4     var seq2 = Enumerable.Range(0, 10)
 5                 .Select(i => i*i);
 6 
 7     //return seq1.Intersect(seq2); //0,1,4,9
 8     //return seq1.Except(seq2); //2,3,5,6,7,8
 9     //return seq1.Concat(seq2); //0,1,2,3,4,5,6,7,8,9,0,1,4,9,16,25,36,49,64,81
10     //return seq1.Concat(seq2).Distinct(); //Delete duplicates
11     return seq1.Union(seq2); //Also delete duplicates
12 }

 

C# LINQ

标签:order   desc   span   null   exce   color   log   union   custom   

原文地址:http://www.cnblogs.com/Dylan-Java-NYC/p/5998981.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!