linq学习笔记(一)
本例演示了如何从一个int数组中找出偶数,并将结果从大小到排序
 using System;
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.Linq;
using System.Linq; using System.Text;
using System.Text;
 namespace LinqDemo
namespace LinqDemo {
{
 class Program
    class Program {
    { static int[] numbers = { 1, 3, 4, 5, 6, 7, 8, 9, 10, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11 };
        static int[] numbers = { 1, 3, 4, 5, 6, 7, 8, 9, 10, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11 };
 static void Main(string[] args)
        static void Main(string[] args) {
        { Traditonal();
            Traditonal(); Console.WriteLine("----------------------");
            Console.WriteLine("----------------------"); LinqMethod();
            LinqMethod(); Console.ReadLine();
            Console.ReadLine();
 
           
 }
        }

 /// <summary>
        /// <summary> /// 传统写法
        /// 传统写法 /// </summary>
        /// </summary> static void Traditonal()
        static void Traditonal()  {
        { 
             List<int> SelectedNumbers = new List<int>();
            List<int> SelectedNumbers = new List<int>(); foreach (int i in numbers)
            foreach (int i in numbers) {
            { if (i % 2==0)
                if (i % 2==0) {
                { SelectedNumbers.Add(i);
                    SelectedNumbers.Add(i);                    
 }
                }                }
            }            
             SelectedNumbers.Sort(SortDesc); //.net1.0写法
            SelectedNumbers.Sort(SortDesc); //.net1.0写法            for (int i = 0; i < SelectedNumbers.Count; i++)
            for (int i = 0; i < SelectedNumbers.Count; i++) {
            { Console.WriteLine(SelectedNumbers[i]);
                Console.WriteLine(SelectedNumbers[i]); }
            } 
             }
        }

 /// <summary>
        /// <summary> /// 逆顺排序(配合传统写法)
        /// 逆顺排序(配合传统写法) /// </summary>
        /// </summary> /// <param name="x"></param>
        /// <param name="x"></param> /// <param name="y"></param>
        /// <param name="y"></param> /// <returns>1(x大于y),0(x等于y),-1(x小于y)</returns>
        /// <returns>1(x大于y),0(x等于y),-1(x小于y)</returns> static int SortDesc(int x,int y)
        static int SortDesc(int x,int y)  {
        { //if (x < y)
            //if (x < y)  //{
            //{ //    return 1;
            //    return 1; //}
            //} //else if (x == y)
            //else if (x == y) //{
            //{ //    return 0;
            //    return 0; //}
            //} //else
            //else  //{
            //{ //    return -1;
            //    return -1; //}//也可以简写为下面的一行
            //}//也可以简写为下面的一行 return y - x;
            return y - x;
 }
        }
 /// <summary>
        /// <summary> /// Linq的写法
        /// Linq的写法 /// </summary>
        /// </summary> static void LinqMethod()
        static void LinqMethod()  {
        { 
             var SelectedNumbers = from number in numbers where (number % 2 == 0) orderby number descending select number;
            var SelectedNumbers = from number in numbers where (number % 2 == 0) orderby number descending select number; foreach (var i in SelectedNumbers)
            foreach (var i in SelectedNumbers) {
            { Console.WriteLine(i);
                Console.WriteLine(i); }
            }           
 }
        }

 
        }
    } }
}可以看出用Linq写法,代码更简洁
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 
                    
                     
                    
                 
                    
                

 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号