开发者

Using Extension methods versus lambda expression in c#

开发者 https://www.devze.com 2023-04-01 05:56 出处:网络
Suppose I have an extension method public static IEnumerable<Product> Filter( this IEnumerable<Product> productEnum,

Suppose I have an extension method

    public static IEnumerable<Product> Filter(
    this IEnumerable<Product> productEnum,
    Func<Product, bool> selectorParam)
    {
        return productEnum.Where(selectorParam);
    }

which I call like so

    Func<Product, bool> languageFilter = prod => prod.Language == lang;

Which if im not misaken is functionally the same as

var products = Products.Where(q => q.Language == parameter);

I'm trying to understand when one might utilize an extension method as per the 1st sample, an开发者_高级运维d when to use the linq syntax.


I think you are confusing some terminology. The Filter method you show is an extension method (indicated as such by the this keyword in the parameter list. What you are terming as "linq syntax" is actually a lambda expression.

All of the LINQ methods are implemented as extension methods and support two "styles" of calling:

  1. Actually call the extension method directly as if they were part of the type being extended. This is what you show when calling it as productEnum.Where(selectorParam).

  2. Using a more SQL-like syntax, called LINQ query syntax. Keep in mind that this SQL-like syntax is translated by the compiler to actually call the extension methods.

When you define Func<Product, bool> languageFilter = prod => prod.Language == lang;, you are actually defining a lambda expression and assigning it to a variable (languageFilter) of type Func<Product, bool>. The Products.Where(q => q.Language == parameter); statement is exactly the same except that rather than capturing the lambda expression in a variable you simply pass it to the extension method.

For your example, to get the same result using the LINQ query syntax you would write it similar to

from p in Products
where p.Language == parameter

(assuming that parameter is defined elsewhere)


I think if there is already an extension method in the framework to do what you want, use that. The 'Where' method is also an extension method, just not one you wrote. My rationale is that 'those clever Microsoft chaps have probably done this better than I ever would'.


Internally, linq syntax is broken into an extension method, so there is functionally no difference.

In your case where you have a Filter extension method, it might not make much sense to use the first form.(which i assume is incomplete in your question, by accident). However, let's assume there's some commonly used logic you need to perform in your Filter method which you would not want to repeat over and over in every instance of a where clause. In that case, it makes sense to build a custom extension method so you can reuse that logic.

For instance, let's assume we only work with products that have not expired(assuming your Product object has an isExpired property). Since, you're likely to be filtering on these unexpired products further with several selectorParam values, you can just use:

public static IEnumerable<Product> Filter(
    this IEnumerable<Product> productEnum,
    Func<Product, bool> selectorParam)
    {
        return productEnum.Where(x => !x.IsExpired).Where(selectorParam);
    }

So instead of calling products.Where(x => !x.IsExpired).Where(q => q.Language == parameter) or products.Where(x => !x.IsExpired).Where(q => q.Price > parameter)

you could just use :
products.Filter(q => q.Language == parameter) and products.Filter(q => q.Price > parameter) respectively.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号