LINQ 查询表达式与流畅(点)表示法
Dim namePriceQuery = From prod In products
Select prod.Name, prod.Price
或
Dim namePriceQuery = products.select(function(x) New With{ Key .newName =x.name _
, Key .newPrice= x.price})
据我所知,唯一的区别是第二个表达式具有 Key 的优点 关键字。两者到底有什么区别??在什么情况下我会选择使用其中一种?一种与另一种相比有哪些优点和缺点?
我假设 C# 语法等效项的工作原理相同,并且应该在前面提到的类似场景中使用。非常感谢!
Dim namePriceQuery = From prod In products
Select prod.Name, prod.Price
OR
Dim namePriceQuery = products.select(function(x) New With{ Key .newName =x.name _
, Key .newPrice= x.price})
To my knowledge the only difference is with with the second expression has the benefits of the Key keyword. What exactly is the difference between the two?? And what scenarios would I choose to use one or the other in? What are the pros and cons of one vs the other?
I am assuming the C# syntax equivalent will work the same and should be used in similar before mention scenarios. Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两者的工作原理都是一样的,但我会选择最具可读性的一个。
Either one works the same but I would choose the one that is most readable.
两种说法都是平等的。使用 linq 语法制定的 Linq 查询将转换为适当的方法调用。
选择在可读性方面最适合您和/或使您的代码更易于理解的内容。如果您得到非常复杂的查询,使用 select Many 并对 linq 语法进行分组会更好读。
我根据情况在两种形式的 linq 查询之间切换。
Both statements are equal. Linq queries formulated with linq syntax are translated into the appropriate method calls.
Select what fits you best in terms of readability and/or makes your code more understandable. If you get really complex queries, using select many and grouping the linq syntax is much better to read.
I switch between both forms of linq queries depending on the situation.