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

后端 未结 13 1411
一向
一向 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:43

    I prefer the query syntax as I came from traditional web programming using SQL. It is much easier for me to wrap my head around. However, it think I will start to utilize the .Where(lambda) as it is definitely much shorter.

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

    I've been using Linq for about 6 months now. When I first started using it I preferred the query syntax as it's very similar to T-SQL.

    But, I'm gradually coming round to the former now, as it's easy to write reusable chunks of code as extension methods and just chain them together. Although I do find putting each clause on its own line helps a lot with readability.

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

    Neither is better: they serve different needs. Query syntax comes into its own when you want to leverage multiple range variables. This happens in three situations:

    • When using the let keyword
    • When you have multiple generators (from clauses)
    • When doing joins

    Here's an example (from the LINQPad samples):

    string[] fullNames = { "Anne Williams", "John Fred Smith", "Sue Green" };
    
    var query =
      from fullName in fullNames
      from name in fullName.Split()
      orderby fullName, name
      select name + " came from " + fullName;
    

    Now compare this to the same thing in method syntax:

    var query = fullNames
      .SelectMany (fName => fName.Split().Select (name => new { name, fName } ))
      .OrderBy (x => x.fName)
      .ThenBy  (x => x.name)
      .Select  (x => x.name + " came from " + x.fName);
    

    Method syntax, on the other hand, exposes the full gamut of query operators and is more concise with simple queries. You can get the best of both worlds by mixing query and method syntax. This is often done in LINQ to SQL queries:

    var query =
      from c in db.Customers
      let totalSpend = c.Purchases.Sum (p => p.Price)    // Method syntax here
      where totalSpend > 1000
      from p in c.Purchases
      select new { p.Description, totalSpend, c.Address.State };
    
    0 讨论(0)
  • 2020-11-22 05:48

    Each style has their pros and cons. Query syntax is nicer when it comes to joins and it has the useful let keyword that makes creating temporary variables inside a query easy.

    Fluent syntax on the other hand has a lot more methods and operations that aren't exposed through the query syntax. Also since they are just extension methods you can write your own.

    I have found that every time I start writing a LINQ statement using the query syntax I end up having to put it in parenthesis and fall back to using fluent LINQ extension methods. Query syntax just doesn't have enough features to use by itself.

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

    The fluent interface if there's just a where. If I need a select or orderby, I generally use the Query syntax.

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

    I really like the Fluent syntax and I try to use it where I can, but in certain cases, for example where I use joins, I usually prefer the Query syntax, in those cases I find it easier to read, and I think some people are more familiar to Query (SQL-like) syntax, than lambdas.

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