c# - 哪个是使用 LINQ 的更好方法?

标签 c# linq

我有这两行,它们做的事情完全一样。但是写的不一样。哪个是更好的做法,为什么?

firstRecordDate = (DateTime)(from g in context.Datas
                                   select g.Time).Min();

firstRecordDate = (DateTime)context.Datas.Min(x => x.Time);

最佳答案

there is no semantic difference between method syntax and query syntax. In addition, some queries, such as those that retrieve the number of elements that match a specified condition, or that retrieve the element that has the maximum value in a source sequence, can only be expressed as method calls.

http://msdn.microsoft.com/en-us/library/bb397947.aspx

另请参阅此处:.NET LINQ query syntax vs method chain

这归结为您对什么感到满意以及您发现什么更具可读性。

关于c# - 哪个是使用 LINQ 的更好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7124169/

相关文章:

c# - 如何检查 EntityTypeConfiguration<T> 实例的成员配置?

c# - int 是枚举的支持类型

c# - try catch 的行为,最后在 c# 中使用 return 语句工作流程

c# - Double.Parse ("NaN") 是否正确解析?

linq - EF4 延迟加载 - 如何强制加载?

linq - Entity Framework (4.0)如何排除相关表

c# - 使用 xamarin 进行原生开发

c# - 可枚举或列表的属性

c# - 带条件 where 子句的 LINQ

c# - 使用 Linq to C# 获取特定属性值