Fluent and Query Expression — Is there any benefit(s) of one over other?

后端 未结 13 1452
一向
一向 2020-11-22 05:15

LINQ is one of the greatest improvements to .NET since generics and it saves me tons of time, and lines of code. However, the fluent syntax seems to come much more natural

相关标签:
13条回答
  • 2020-11-22 05:49

    I have just set up our company's standards and we enforce the use of the Extension methods. I think it's a good idea to choose one over the other and don't mix them up in code. Extension methods read more like the other code.

    The comprehension syntax does not have all operators and using parentheses around the query and add extension methods after all just begs me for using extension methods from the start.

    But for the most part it is just personal preference with a few exceptions.

    0 讨论(0)
  • 2020-11-22 05:55

    Fluent syntax does seem more powerful indeed, it should also work better for organizing code into small reusable methods.

    0 讨论(0)
  • 2020-11-22 05:59

    I prefer to use the latter (sometimes called "query comprehension syntax") when I can write the whole expression that way.

    var titlesQuery = from e in entries
                      where e.Approved
                      orderby e.Rating
                      select e.Titles;
    
    var title = titlesQuery.FirstOrDefault();
    

    As soon as I have to add (parentheses) and .MethodCalls(), I change.

    When I use the former, I usually put one clause per line, like this:

    var title = entries
        .Where (e => e.Approved)
        .OrderBy (e => e.Rating)
        .Select (e => e.Title)
        .FirstOrDefault();
    

    I find that a little easier to read.

    0 讨论(0)
  • 2020-11-22 05:59

    I know this question is tagged with C#, but the Fluent syntax is painfully verbose with VB.NET.

    0 讨论(0)
  • 2020-11-22 06:03

    In VB.NET i very much prefer query syntax.

    I hate to repeat the ugly Function-keyword:

    Dim fullNames = { "Anne Williams", "John Fred Smith", "Sue Green" };
    Dim query =
         fullNames.SelectMany(Function(fName) fName.Split().
         Select(Function(Name) New With {Name, fName})).
         OrderBy(Function(x) x.fName).
         ThenBy(Function(x) x.Name).
         Select(Function(x) x.Name & " came from " & x.fName)
    

    This neat query is much more readable and maintainable in my opinion:

    query = From fullName In fullNames
            From name In fullName.Split()
            Order By fullName, name
            Select name & " came from " & fullName
    

    VB.NET's query syntax is also more powerful and less verbose than in C#: https://stackoverflow.com/a/6515130/284240

    For example this LINQ to DataSet(Objects) query

    VB.NET:

    Dim first10Rows = From r In dataTable1 Take 10
    

    C#:

    var first10Rows = (from r in dataTable1.AsEnumerable() 
                       select r)
                       .Take(10);
    
    0 讨论(0)
  • 2020-11-22 06:03

    I don't get the query syntax at all. There's just no reason for it in my mind. let can be acheived with .Select and anonymous types. I just think things look much more organized with the "punctuation" in there.

    0 讨论(0)
提交回复
热议问题