扩展方法的语法VS查询语法语法、方法、VS

2023-09-02 20:43:26 作者:陌笑已徒然

我想要得到,如果有一个很好的时间来使用标准的LINQ关键字或LINQ扩展方法与拉姆达EX pressions上的手柄。他们似乎做同样的事情,只是被写入不同。它是风格纯粹是怎么回事?

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, just are written differently. Is it purely a matter of style?

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

// or with extension methods:
var query = Products
    .Where(p => p.Name.Contains("foo")
    .OrderBy(p => p.Name);

他们是非常相似的第二个例子是有点更简洁,但也许少EX pressive,如果你不知道什么是=>在做什么。

They're very similar with the second example being a bit more terse, but perhaps less expressive if you don't know what the => is doing.

除了创作简洁code,是否还有其他的优势,使用扩展方法,而不是在LINQ语法?

推荐答案

说实话,有时也可以,一旦你开始使用funcs中和动作是情景。说你是使用这三种funcs中:

Honestly, sometimes it can be situational once you start using Funcs and Actions. Say you are using these three funcs:

  Func<DataClasses.User, String> userName = user => user.UserName;
  Func<DataClasses.User, Boolean> userIDOverTen = user => user.UserID < 10;
  Func<DataClasses.User, Boolean> userIDUnderTen = user => user.UserID > 10;

正如你所看到的第一个替换lamdba EX pression来获取用户名,第二个取代用于检查ID低于10,让我们面对它lamdba EX pression,第三个应该是pretty的容易,现在理解了。

As you can see the first one replaces the lamdba expression to get the user name, the second replaces a lamdba expression used to check if the ID is lower than 10, and let's face it, the third should be pretty easy to understand now.

注:这是一个愚蠢的例子,但它的工作原理

NOTE: This is a silly example but it works.

  var userList = 
    from user in userList
    where userIDOverTen(user)
    select userName;

对战

  var otherList =
    userList
    .Where(IDIsBelowNumber)
    .Select(userName)

在这个例子中,第二个是少一点麻烦,因为扩展方法可以充分利用函数功能的,但他的LINQ EX pression不能因为它看起来只是一个布尔值,而不是一个函数功能即返回boolean值。但是,这是它可能会更好使用前pression语言。假设你已经有了一个方法,它的不仅仅是一个用户:

In this example, the second is a little less verbose since the extension method can make full use of the Func, but he Linq expression can't since it is look just for a Boolean rather than a Func that returns boolean. However, this is where it might be better to use the expression language. Say you already had a method that takes in more than just a user:

  private Boolean IDIsBelowNumber(DataClasses.User user, 
          Int32 someNumber, Boolean doSomething)
  {
    return user.UserID < someNumber;
  }

注:DoSomething的是,因为在那里扩展方法是确定一个方法,它在用户和整数并返回布尔就在那里。一种恼人的这个例子。

Note: doSomething is just there because of the where extension method being ok with a method that takes in a user and integer and returns boolean. Kind of annoying for this example.

现在,如果你看一下LINQ查询:

Now if you look at the Linq query:

  var completeList =
     from user in userList
     where IDIsBelowNumber(user, 10, true)
     select userName;

你是好它。现在扩展方法:

You're good for it. Now the Extension Method:

  var otherList =
    userList
    .Where(IDIsBelowNumber????)
    .Select(userName)

如果没有一个lambda EX pression,我真的不能调用该方法。所以,现在我要做的就是创建一个基于关闭原来的方法调用创建一个函数功能的方法。

Without a lambda expression, I really can't call that method. So now what I have to do is create a method that creates a Func based off the original method call.

   private Func<DataClasses.User, Boolean> IDIsBelowNumberFunc(Int32 number)
   {
      return user => IDIsBelowNumber(user, number, true);
   }

和然后将其插入:

  var otherList =
     userList
     .Where(IDIsBelowNumberFunc(10))
     .Select(userName)

所以你可以看到,有时也可能是更容易使用,在时间的查询方法。

So you can see, sometimes it may just be easier to use the query approach at times.