C# Linq 查询或 Lambda 表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16185514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:17:43  来源:igfitidea点击:

Linq query or Lambda expression?

c#linqlambda

提问by Nalaka526

I'm using Entity Framework in my ASP.NET, C#, Web Application. If I need to select a record from DataBase (in Data Access Layer), which method should I use? Linq query or a Lambda Expression?

我在我的 ASP.NET、C#、Web 应用程序中使用实体框架。如果我需要从数据库(在数据访问层)中选择一条记录,我应该使用哪种方法?Linq 查询还是 Lambda 表达式?

Eg:-

例如:-

//Linq        
var result = from a in db.myTable.Take(1) where a.Id == varId  select a;
return result.First();

//Lambda
return db.myTable.FirstOrDefault(a => a.Id == varId);

Is there any preferred way in this scenario or any advantage over the other?

在这种情况下是否有任何首选方式或比其他任何优势?

采纳答案by Habib

Query Expression compiles into Method Expression (Lambda expression), so there shouldn't be any difference, In your code though you are accessing Firstand FirstOrDefaultwhich would behave differently.

查询表达式编译为方法表达式(Lambda 表达式),因此应该没有任何区别,在您的代码中,尽管您正在访问First并且FirstOrDefault其行为会有所不同。

See: Query Syntax and Method Syntax in LINQ (C#)

请参阅:LINQ (C#) 中的查询语法和方法语法

and LINQ Query Expressions (C# Programming Guide)

LINQ 查询表达式(C# 编程指南)

At compile time, query expressions are converted to Standard Query Operator method calls according to the rules set forth in the C# specification. Any query that can be expressed by using query syntax can also be expressed by using method syntax. However, in most cases query syntax is more readable and concise.

在编译时,查询表达式会根据 C# 规范中规定的规则转换为标准查询运算符方法调用。任何可以用查询语法表达的查询也可以用方法语法表达。但是,在大多数情况下,查询语法更具可读性和简洁性。

回答by Ipad

I guess the result is the same. Lambda is just a bit more comfortable. If you need a result of just one table, the lambda expression is very fast and readable.

我想结果是一样的。Lambda 只是稍微舒服一点。如果您只需要一张表的结果,则 lambda 表达式非常快速且可读。

回答by Sergey Berezovskiy

Linq query syntax is just a syntax sugar for expression methods. Any Linq query compiled into expression methods. Btw your first query:

Linq 查询语法只是表达式方法的语法糖。任何编译为表达式方法的 Linq 查询。顺便说一句,您的第一个查询:

var query = from a in db.myTable.Take(1) 
            where a.Id == varId  
            select a;
return query.First();

Is equivalent to

相当于

return db.myTable.Take(1).Where(a => a.Id == varId).First();

回答by nvoigt

Both of your tries use Linq.

您的两次尝试都使用 Linq。

The first takes one record and checks if the id matches.

第一个获取一条记录并检查 id 是否匹配。

The second takes the first record where the id matches.

第二个采用 id 匹配的第一条记录。

That's a difference.

这是一个区别。

回答by Nico

Every query expression can be expressed as C#-code using calls to query operators as extension methods. But the opposite is not true; only a small subset of the standard query operators can be used as keywords in query expressions. In other words query expressions have some limitations that the method-call mechanism does not have:

每个查询表达式都可以表示为 C# 代码,使用对查询运算符的调用作为扩展方法。但事实并非如此。只有一小部分标准查询运算符可以用作查询表达式中的关键字。换句话说,查询表达式有一些方法调用机制没有的限制:

  1. Some query operators have simply no C# query expression equivalent, e.g. ToArray().
  2. We can't use all kinds of overloads in C#'s query expressions. E.g. there is an overload of Select() that awaits the index of the currently iterated object; you cannot call this overload within a query expression.
  3. We can't use statement lambdas in query expressions. - This is the cause why object and collection initializers have been introduced into the C# language.
  1. 一些查询运算符根本没有等效的 C# 查询表达式,例如 ToArray()。
  2. 我们不能在 C# 的查询表达式中使用所有类型的重载。例如,有一个 Select() 重载等待当前迭代对象的索引;您不能在查询表达式中调用此重载。
  3. 我们不能在查询表达式中使用语句 lambda。- 这就是为什么在 C# 语言中引入对象和集合初始值设定项的原因。