關於Lambda

LINQ系列

這次要講的是Lambda...

蛤...這個溝系三小...你應該會是以下的表情狀況,我好多好多不懂,我好難理解...

暈倒...



沒關係,我們就從前世今身,開始講起,撰寫LINQ,若用到指定準則或選擇資料的時候

,就會使用到Lambda,舉例我們很常用的例子
 products.Where(x => x.ProductId == "1");

 在調用Enumerable.where方法時候,就會縮小或篩選出現有的集合內容。


這代表用單一語法,可以表達方法的定義、宣告,執行它的委派過程。EX:匿名方法、投影(後續有時間再談)

以下是很單純的lambda運算式

x => x * 21

上述的意思是:用x做為此函數的參數,x前往(goes to)的結果是 x * 21,那=>代表的解讀是前往

若上述轉換成投影如下

x => new { number =  x * 21 };

接者我們再把它寫成C# 1.0

    class Program
    {
        public delegate int IncreaseByAnumber(int x);
        static public int MultiplyByANumber(int x)
        {
            return x * 21;
        }

        private static void Main(string[] args)
        {
            IncreaseByAnumber increase = new IncreaseByAnumber(MultiplyByANumber);
            Console.WriteLine(increase(10));  //210
        }
    }

若遇到C# 2.0匿名方法,改寫如下


        public delegate int IncreaseByAnumber(int x);
        private static void Main(string[] args)
        {
            IncreaseByAnumber increase = new IncreaseByAnumber(delegate(int i)
            {
                return i * 21;
            });
            Console.WriteLine(increase(10));
        }

接者改寫成現在的lambda如下

        public delegate int IncreaseByAnumber(int x);
        private static void Main(string[] args)
        {
            IncreaseByAnumber increase = x => x * 21;
            Console.WriteLine(increase(10));
        }

以上這就是Lambda sample簡化過程。

稍微加點變化,加入Func用法,在訂房需求當中如果大人消費
滿一萬則打9折,小孩滿兩千則打8折...這時候我用lambda方式去撰寫

    class Program
    {
        class BookingOrder
        {
            private Func<int, int> discount;
            public BookingOrder(Func<int, int> discount)
             => this.discount = discount;
        
            public int getDiscount(int total) => discount(total);
        }

        private static void Main(string[] args)
        {
            //寫法一
            //Func<int, int> adoultDiscount = delegate(int amount)
            //    {
            //        return (int) (amount >= 10000 ? amount * 0.9 : amount);
            //    };
            //Func<int, int> childDiscount = delegate(int amount)
            //    {
            //        return (int)(amount >= 2000 ? amount * 0.8 : amount);
            //    };
            //寫法二
             Func<int, int> adoultDiscount = (int amount)
                            => (int)(amount >= 10000 ? amount * 0.9 : amount);
             Func<int, int> childDiscount = (int amount) 
                            => (int) (amount >= 2000?amount * 0.8:amount);

            BookingOrder bookingOrder1  = new BookingOrder(adoultDiscount);
            BookingOrder bookingOrder2 = new BookingOrder(childDiscount);

            int total = 5000;
            Console.WriteLine(bookingOrder1.getDiscount(total));
            Console.WriteLine(bookingOrder2.getDiscount(total));

            total = 12000;
            Console.WriteLine(bookingOrder1.getDiscount(total));
            Console.WriteLine(bookingOrder2.getDiscount(total));
        }
    }



關於委派參考  http://vito-note.blogspot.com/2012/03/delegate.html

元哥的筆記