Extension methods syntax vs query syntax

后端 未结 7 1077
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 12:42

I\'m trying to get a handle on if there\'s a good time to use standard linq keywords or linq extension methods with lambda expressions. They seems to do the same thing, jus

相关标签:
7条回答
  • 2020-11-27 13:25

    One advantage of extension methods/lynda expressions is the additional operators that is offered like Skip and Take. For example, if you are creating a pagination method, being able to skip the first 10 records and take the next 10 is easy to implement.

    0 讨论(0)
  • 2020-11-27 13:30

    They compile the same, and are equivalent. Personally, I prefer the lambda (extension) methods for most things, only using the statements (standard) if I'm doing LINQ to SQL or otherwise trying to emulate SQL. I find that the lambda methods flow better with code, whereas the statements are visually distracting.

    0 讨论(0)
  • 2020-11-27 13:36

    I like to use the query syntax when its really a query, ie a lazy expression which evaluates on demand.

    A method that looks like regular method calls (method syntax or the lambda syntax) doesn't look lazy enough, so I use that as a convention. For eg,

    var query = from p in Products
                where p.Name.Contains("foo")
                orderby p.Name
                select p;
    
    var result = query.ToList(); //extension method syntax
    

    If it's a not a query, I like the fluent style which looks to me consistent with other eagerly executing calls.

    var nonQuery = Products.Where(p => p.Name.Contains("foo"))
                           .OrderBy(p => p.Name)
                           .ToList();
    

    It helps me to differentiate the two styles of calls better. Of course there are situations where you will be forced to use method syntax anyway, so my convention is not very compelling enough.

    0 讨论(0)
  • 2020-11-27 13:37

    I think it's a good idea not to use them together and choose one and stick with it.

    Mostly it's personal taste, but in the query syntax (Comprehension method) not all operators are available as was said before.

    I find the Extension Methods syntax more in line with the rest of my code. I do my SQL in SQL. It's also very easy to build your expression just by adding everything on top of eachother with the extension methods.

    Just my two cents.

    As I cannot make comments yet I want to make one here to the answer of Programming Tool: Why make a whole new method for the last example?? Can't you just use:

    .Where(user => IDIsBelowNumber(user, 10, true))

    0 讨论(0)
  • 2020-11-27 13:42

    One advantage to using LINQ extension methods (method-based queries) is that you can define custom extension methods and it will still read fine.

    On the other hand, when using a LINQ query expression, the custom extension method is not in the keywords list. It will look a bit strange mixed with the other keywords.

    Example

    I am using a custom extension method called Into which just takes a string:

    Example with query

    var query = (from p in Products
        where p.Name.Contains("foo")
        orderby c.Name
        select p).Into("MyTable");
    

    Example with extension methods

    var query = Products
                       .Where(p => p.Name.Contains("foo"))
                       .OrderBy(p => p.Name)
                       .Into("MyTable");
    

    In my opinion the latter, using a method-based query, reads better when you have custom extension methods.

    0 讨论(0)
  • 2020-11-27 13:45

    I prefer the extension method syntax when I use Linq methods that have no query syntax equivalent, such as FirstOrDefault() or others like that.

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