|
|
<div id="cnblogs_post_body">前言                    
       边工作边转型,对象的思想对于初学者是非常重要的。具备一定的代码识别和理解能力后,我决定边做项目边学习。前些阶段小项目中查询数据的时候使用了Linq,那就从它开始。
正文                 
1、对于数组:
<div class="csharpcode"> <div id="codeSnippetWrapper"> <div id="codeSnippet" class="csharpcode"> 1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: using System.Threading.Tasks; 6:  7: namespace ConsoleApplication1 8: { 9: class LinqTest 10: { 11: static void Main(string[] args) 12: { 13: string[] names = { &quot;Alone&quot;,&quot;Lisi&quot;,&quot;Samba&quot;,&quot;Fatimah&quot;,&quot;Sam&quot;,&quot;Ssn&quot;}; 14: //第一种使用原生态的查询语句的方法 15: var queryResult = from n in names 16: where n.StartsWith(&quot;S&quot;) 17: orderby n descending 18: select n; 19: 20: //第二种方法使用Linq的方法 21: // var queryResult = names.OrderByDescending(n=>n).Where(n=>n.StartsWith(&quot;s&quot;)); 22: 23: foreach (var Items in queryResult) 24: { 25: Console.WriteLine(Items); 26: } 27: Console.ReadKey(); 28:  29: } 30: } 31: } |
|