Lambda開啟新的編寫方式

來源:互聯網
上載者:User
      出於對Lambda的好奇所以學習了一下,看能不能通過Lambda來開啟新的更用效的代碼編寫方式。以下是通過Lambda實現一個通用型的遞迴控制處理函數。這隻是一個參考,實際上你可以挖掘Lambda更多的使用方法.多謝 裝配腦袋  指出問題重新調整一下代碼(這裡本意不是描述該方法功能如何,而是想體現Lambda的引發的雖一種編碼方式). using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace lambdaTest
{
    public delegate void EventExecute<T>(T source, Program.Recursive<T> next);
    public static class Program
    {
        static void Main(string[] args)
        {

            UnderList ul = new UnderList();
            ul.EmployeeID = 2;
            string sql = "select EmployeeID,FirstName from Employees where ReportsTo={0}";
            using (System.Data.SqlClient.SqlConnection conn
                = new System.Data.SqlClient.SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True"))
            {
                conn.Open();
                Do<UnderList>(ul, p => (p.EmployeeID > 0),
                   delegate(UnderList source, Program.Recursive<UnderList> r)
                   {
                       source.Format += "\t";
                       List<int> mUnders = new List<int>();
                       SqlCommand cmd = conn.CreateCommand();
                       cmd.CommandText = string.Format(sql, source.EmployeeID);
                       using (SqlDataReader reader = cmd.ExecuteReader())
                       {
                           while (reader.Read())
                           {
                               mUnders.Add((int)reader[0]);
                               Console.WriteLine(source.Format + reader[1]);

                           }

                       }
                       source.EmployeeID = 0;
                       foreach (int item in mUnders)
                       {
                           source.EmployeeID = item;
                           r.Do();

                       }
                   }
                );
            }
     
           Console.Read();
            
            
         }
        private static void Do<T>(T i, Func<T, bool> a, EventExecute<T> e)
        {

            Program.Recursive<T> rec = new Recursive<T>(i,e,a);
            e(i, rec);
            
        }
        public class Recursive<T>
        {
            public Recursive(T source, EventExecute<T> e, Func<T, bool> expression)
            {
                Source = source;
                E = e;
                Expression = expression;
            }
            private T Source;
            private EventExecute<T> E;
            Func<T, bool> Expression;
            
            public void Do()
            {
                if (Expression(Source))
                    E(Source, this);
            }
            
           

        }
    }
    public class UnderList
    {
        private int mEmployeeID;
        public int EmployeeID
        {
            get
            {
                return mEmployeeID;
            }
            set
            {
                mEmployeeID = value;
            }
        }
        public string Format = "";
        
        

    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.