扩展方法语法与查询语法 [英] Extension methods syntax vs query syntax

查看:44
本文介绍了扩展方法语法与查询语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解是否有时间将标准 linq 关键字或 linq 扩展方法与 lambda 表达式一起使用.他们似乎做同样的事情,只是写法不同.纯粹是风格问题吗?

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);

它们非常相似,第二个示例更简洁一些,但如果您不知道 => 在做什么,则表达能力可能会降低.

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.

除了编写简洁的代码之外,与 LINQ 语法相比,使用扩展方法还有其他优势吗?

推荐答案

老实说,一旦您开始使用 Funcs 和 Actions,有时可能会遇到一些情况.假设您正在使用这三个函数:

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 表达式来获取用户名,第二个替换了一个用于检查 ID 是否小于 10 的 lamdba 表达式,让我们面对现实吧,第三个应该很容易理解现在.

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)

在这个例子中,第二个不那么冗长,因为扩展方法可以充分利用 Func,但 Linq 表达式不能,因为它只是寻找一个 Boolean 而不是一个返回布尔值的 Func.但是,这就是使用表达式语言可能更好的地方.假设您已经有一个方法不仅可以接收用户:

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 就在那里,因为 where 扩展方法可以接受用户和整数并返回布尔值的方法.这个例子有点烦人.

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 表达式,我真的无法调用该方法.所以现在我要做的是创建一个基于原始方法调用创建 Func 的方法.

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.

这篇关于扩展方法语法与查询语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆