zoukankan      html  css  js  c++  java
  • 转 LINQ表达式

    我是白痴,所以我不知道什么是lambda,看了网上N篇文章仍然还是个白痴。还不知道。于是。
    给自己写篇自己能看得懂的文章,希望以后再白痴的时候看看就不白痴了。

    委托

    委托是一种引用方法的类型。一旦为委托分配了方法,委托将与该方法具有完全相同的行为。委托方法的使用可以像其他任何方法一样,具有参数和返回值。
    委托就是给一个函数起一个别的名字。

    using System;
    // Declare delegate -- defines required signature:
    delegate void SampleDelegate(string message);

    class MainClass
    {
        // Regular method that matches signature:
        static void SampleDelegateMethod(string message)
        {
            Console.WriteLine(message);
        }

        static void Main()
        {
            // Instantiate delegate with named method:
            SampleDelegate d1 = SampleDelegateMethod;
            // Instantiate delegate with anonymous method:
            SampleDelegate d2 = delegate(string message)
            { 
                Console.WriteLine(message); 
            };

            d1("Hello");
            
            d2("World,http://www.dc9.cn/");
        }
    }

    这玩意也是委托

    public Form1()
    {
        InitializeComponent();

         this.Click += this.MultiHandler; 
    }

    private void MultiHandler(object sender, System.EventArgs e)
    {
        MessageBox.Show(((MouseEventArgs)e).Location.ToString());
    }


    匿名方法

    匿名方法就是没名儿的委托。虽然没名,但是必须加”delegate“来表示我没名。

    // Create a delegate instance
    delegate void Del(int x);

    // Instantiate the delegate using an anonymous method
    Del d = delegate(int k) { /* ... */ };

    Del d = delegate() { System.Console.WriteLine("Copy #:{0}", ++n); };

    delegate void Printer(string s);

    Printer p = delegate(string j)
            {
                System.Console.WriteLine(j);
            };

    p("The delegate using the anonymous method is called.");

    delegate string Printer(string s);
            private void button1_Click(object sender, EventArgs e)
            {

                         Printer p =delegate(string j)
                        {
                           return (j)+"烦死了";
                        };

                Console.WriteLine(p("The delegate using the anonymous method is called."));


            }

    或者有名。。。。的匿名委托。。。。哈哈,我在抽风还是微软在抽风。让我死吧。

    delegate void Printer(string s);
            private void button1_Click(object sender, EventArgs e)
            {
                Printer p = new Printer(Form1.DoWork);
                p("http://www.dc9.cn/");
            }
            static void DoWork(string k)
            {
                System.Console.WriteLine(k);
            }

    匿名方法就完了。
    Lamdba 就是 (int x)=>{x+1}就是这样的。。。例子呢就是上面写了的一段用lambda就这样写

    delegate string Printer(string s);
            private void button1_Click(object sender, EventArgs e)
            {

                Printer p = j => j+"烦死了!!!";

                Console.WriteLine(p("The delegate using the anonymous method is called."));


            }

    还能这么用
    和上面那个一样,就是简化了。

    public Form1()
    {
        InitializeComponent();

         this.Click += (s, e) => { MessageBox.Show(((MouseEventArgs)e).Location.ToString());};
    }


    超难+===>?其实不难。难的是转换成一般的函数怎么写呢????
    。。。。。
    Where是Enumerable的一个方法。3.5才有的。里面的参数是Func<(Of <(T, TResult>)>) 泛型委托

    Func<(Of <(T, TResult>)>) 泛型委托

    using System;

    delegate string ConvertMethod(string inString);

    public class DelegateExample
    {
       public static void Main()
       {
          // Instantiate delegate to reference UppercaseString method
          ConvertMethod convertMeth = UppercaseString;
          string name = "Dakota";
          // Use delegate instance to call UppercaseString method
          Console.WriteLine(convertMeth(name));
       }

       private static string UppercaseString(string inputString)
       {
          return inputString.ToUpper();
       }
    }

    写成泛型委托是

    using System;

    public class GenericFunc
    {
       public static void Main()
       {
          // Instantiate delegate to reference UppercaseString method
          Func<string, string> convertMethod = UppercaseString;
          string name = "Dakota";
          // Use delegate instance to call UppercaseString method
          Console.WriteLine(convertMethod(name));
       }

       private static string UppercaseString(string inputString)
       {
          return inputString.ToUpper();
       }
    }


    于是应用到Linq,再变换到lambda

    delegate bool TestFunc(string fruit);
            private void button1_Click(object sender, EventArgs e)
            {

                List<string> fruits =new List<string> { "apple", "http://www.dc9.cn", "banana", "mango", 
                        "orange", "blueberry", "grape", "strawberry" };
                TestFunc f = new TestFunc(DoWork);
                Func<string, bool> f2 = DoWork;
                IEnumerable<string> query = fruits.Where(f2);
                foreach (string fruit in query)
                Console.WriteLine(fruit);
           }

            private static bool DoWork(string k)
            {
              return   k.Length < 6;
           }

    能用。

    delegate bool TestFunc(string fruit);
            private void button1_Click(object sender, EventArgs e)
            {

                List<string> fruits =new List<string> { "apple", "passionfruit", "banana", "mango", 
                        "orange", "blueberry", "grape", "http://www.dc9.cn" };
                TestFunc f = DoWork;
                Func<string, bool> f2 =k=> k.Length < 6;
                IEnumerable<string> query = fruits.Where(f2);
                foreach (string fruit in query)
                Console.WriteLine(fruit);
           }

            private static bool DoWork(string k)
            {
              return   k.Length < 6;
           }

    也能用

    private void button1_Click(object sender, EventArgs e)
            {

                List<string> fruits =new List<string> { "apple", "passionfruit", "banana", "mango", 
                        "orange", "blueberry", "grape", "http://www.dc9.cn" };
                

                IEnumerable<string> query = fruits.Where(
                    delegate(string k){
                        return k.Length < 6;
                    }
                    );
                foreach (string fruit in query)
                Console.WriteLine(fruit);
           }

    能用~

    private void button1_Click(object sender, EventArgs e)
            {

                List<string> fruits =new List<string> { "apple", "passionfruit", "banana", "mango", 
                        "orange", "blueberry", "grape", "http://www.dc9.cn" };
                

                IEnumerable<string> query = fruits.Where(k=>k.Length<6);
                foreach (string fruit in query)
                Console.WriteLine(fruit);
           }

    最后,lambda,能用~~~~就酱紫了~~~~
    于是,我这个白痴,被自己写的教程搞明白了。
    于是我也会了。
    但是心情依旧很糟糕。
    。。。。。。。。。
    ~~~~~~~~~
    啊啊啊啊啊~最后来个有意思的!!

          public delegate int mydg(int a, int b);

         

          public static class LambdaTest

          {

              

              public static int oper(this int a, int b, mydg dg)

              {

                  return dg(a, b);

              }

          }

     

    Console.WriteLine(1.oper(2, (a, b) => a + b));

    Console.WriteLine(2.oper(1, (a, b) => a - b));

  • 相关阅读:
    如何使 Postgresql 的psql 使用 中文提示信息
    Makfile中的 依赖关系
    防范计算机木马入侵反恶意联盟落户滨海 狼人:
    印度最大软件开发商塔塔咨询官网首页被黑 狼人:
    河北银行:用CDP保障业务系统的故障快速恢复 狼人:
    甲骨文11g数据库爆零日攻击 可完全攻破 狼人:
    Adobe Reader与IE 领衔漏洞榜 狼人:
    黑客藏毒于火狐插件 目标瞄准Windows系统 狼人:
    网络订票当心三类陷阱 最好当场识别真伪 狼人:
    微软基于云计算免费杀毒软件Morro曝光 狼人:
  • 原文地址:https://www.cnblogs.com/windphoenix/p/1353234.html
Copyright © 2011-2022 走看看