Linq and lambda expression

前端 未结 3 577
盖世英雄少女心
盖世英雄少女心 2020-12-18 08:56

What is the difference between LINQ and Lambda Expressions? Are there any advantages to using lambda instead of linq queries?

3条回答
  •  被撕碎了的回忆
    2020-12-18 09:35

    Linq is language integrated query. When using linq, a small anonymous function is often used as a parameter. That small anonymous function is a lambda expression.

    var q = someList.Where(a => a > 7);
    

    In the above query a => a > 7 is a lambda expression. It's the equivalent of writing a small utility method and passing that to Where:

    bool smallMethod(int value)
    {
      return value > 7;
    }
    
    // Inside another function:
    var q = someList.Where(smallMethod);
    

    This means that your question is really not possible to answer. Linq and lambdas are not interchangeable, rather lambdas are one of the technologies used to implement linq.

提交回复
热议问题