3

What query am I able to perform with Lambda(Method Syntax) and not with (Query Syntax)?

The questions is simple, this is an example of both:

    int[] numbers = { 5, 10, 8, 3, 6, 12};

    //Query syntax:
    IEnumerable<int> numQuery1 = 
        from num in numbers
        where num % 2 == 0
        orderby num
        select num;

    //Method syntax:
    IEnumerable<int> numQuery2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n);

What would be a query that I can perform Method Syntax and not in the Query Syntax?

4

4 に答える 4

5

(This is a shorter answer than I was originally writing, but other answers have already provided some of the details.)

There are two reasons one might wish to use lambda expressions instead of query expressions:

  • Operations which simply aren't covered by query expressions, whether that's methods which aren't covered at all (e.g. Count) or overloads of operations which are included in query expressions, but not in that form. For example:

    var indexedValues = values.Select((value, index) => new { value, index });
    

    There's no query expression form which uses that overload.

  • When the lambda form is simpler. For example, if you've only got a single projection or filter, it can be simpler to do it in one call than set up the fluff of a query expression:

     var adults = people.Where(person => person.Age > 18);
    

    Vs:

     var adults = from person in people
                  where person.Age > 18
                  select person;
    

    Additionally, the lambda expression approach is somewhat tidier when you want to continue the expression after the query. For example, creating a list of the names of adults:

     var names = people.Where(person => person.Age > 18)
                       .Select(person => person.Name)
                       .ToList();
    

    Vs:

     var names = (from person in people
                  where person.Age > 18
                  select person.Name).ToList();
    

    The brackets end up being a bit irritating.

Where query expressions shine is in the operations which introduce transparent identifiers - joins, SelectMany, let etc. While you obviously can translate such code into lambda form, it can end up really ugly.

于 2013-03-22T06:51:47.230 に答える
4

You can find complete list of methods that have corresponding keywords within query expression syntax here: Query Expression Syntax for Standard Query Operators. List contains information about both C# and VB.NET support. Any method that is not listed there is not available neither in C# nor VB.NET.

Interesting thing is, that there is a bit more available in VB.NET then in C#.

于 2013-03-21T21:50:07.510 に答える
2

You can't do a select top 10 in query syntax. You can use .Take(10) in method syntax.

Aggregate functions are not supported either.

于 2013-03-21T21:44:14.333 に答える
0

LINQ query syntax translates directly to extension method syntax. However, the reverse is not true. There are way more extension methods than supported by the LINQ query syntax keywords.

So, with method syntax you can do exactly the same or more than with LINQ query syntax. Usually LINQ query syntax is nicer to read.

// LINQ query syntax
var query = 
    from num in numbers
    where num % 2 == 0
    orderby num
    select num;

// Corresponding method syntax
var query = numbers
    .Where(num => num % 2 == 0)
    .OrderBy(num => num)
    .Select(num => num);

A simple example of what you can do with method syntax and not with query syntax (since there are no keywords for it):

var query = numbers
    .First();

You can combine both if you want:

var query = 
    (from num in numbers
    where num % 2 == 0
    orderby num
    select num)
    .First();

Apart from First, there are tons of LINQ extension methods (Sum, Average, Max...) that have no keywords in LINQ query syntax.


By the way, because LINQ query syntax maps to extension methods, you are not restricted to using LINQ extension methods. If you define on your own custom objects a Select and Where method that both accept a delegate, then you can use LINQ query syntax on your own objects! How cool is that? For example, a nonsensical example:

public class MyClass
{
    public MyClass Where(Func<string, bool> function)
    {
        return this;
    }

    public List<string> Select(Func<string, string> function)
    {
        return new List<string>();
    }
}

MyClass myClass = new MyClass();
var x = from m in myClass
        where !String.IsNullOrEmpty(m)
        select m;
于 2013-03-21T21:44:30.763 に答える