Linq and lambda expression

前端 未结 3 580
盖世英雄少女心
盖世英雄少女心 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:33

    LINQ is Language integrated query, where is lamda expression are similar to Annonymous method for .Net 2.0.

    You can't really compare them may be you are confused because LINQ is associated with lamda expression most of the time.

    You need to see this article: Basics of LINQ & Lamda Expressions

    EDIT: (I am not so sure, but may be you are looking for the difference between Query Syntax and Method Sytnax)

    int[] numbers = { 5, 10, 8, 3, 6, 12};
    
    //Query syntax:
    IEnumerable numQuery1 = 
        from num in numbers
        where num % 2 == 0
        orderby num
        select num;
    
     //Method syntax:
     IEnumerable numQuery2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n);
    

    In the above example taken from MSDN, Method Sytnax contains a lamda expression (num => num % 2 == 0) which works like a method, takes number as input and returns true if they are even.

    They both are similar, and in the words of Jon Skeet, they both compile to similar code.

提交回复
热议问题