lambda 表达式或
我有以下 LINQ to SQL 查询表达式
from msg in TblUserMessages
join user in Aspnet_Users on msg.FromUserID equals user.UserId
select new {
msg.FromUserID,
msg.ToUserID,
msg.MessageLocationID,
msg.MessageID,
user.UserName
}
和以下 LINQ 方法表达式:
TblUserMessages
.Join (
Aspnet_Users,
msg => msg.FromUserID,
user => user.UserId,
(msg, user) =>
new
{
FromUserID = msg.FromUserID,
ToUserID = msg.ToUserID,
MessageLocationID = msg.MessageLocationID,
MessageID = msg.MessageID,
UserName = user.UserName
}
)
这两个都返回相同的结果集。例如:
82522f05-2650-466a-a430-72e6c9fb68b7
6b2a174a-8141-43d2-b3ad-5b199bcbfcae
1
1
waheed
哪个更好用。 FIRST
一个或 SECOND
一个。
谢谢
I have following LINQ to SQL query expression
from msg in TblUserMessages
join user in Aspnet_Users on msg.FromUserID equals user.UserId
select new {
msg.FromUserID,
msg.ToUserID,
msg.MessageLocationID,
msg.MessageID,
user.UserName
}
And following LINQ method expression:
TblUserMessages
.Join (
Aspnet_Users,
msg => msg.FromUserID,
user => user.UserId,
(msg, user) =>
new
{
FromUserID = msg.FromUserID,
ToUserID = msg.ToUserID,
MessageLocationID = msg.MessageLocationID,
MessageID = msg.MessageID,
UserName = user.UserName
}
)
Both of these return same result set. Which is e.g. :
82522f05-2650-466a-a430-72e6c9fb68b7
6b2a174a-8141-43d2-b3ad-5b199bcbfcae
1
1
waheed
Which one is better to use. The FIRST
one or the SECOND
one.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
它们是等价的。它们不只是返回相同的结果集 - 它们编译为相同的代码。
根据可读性,对个别情况使用查询表达式或点表示法。对于连接,我发现点表示法相当麻烦 - 但我将它用于只有一两个子句(通常是 where/select)的情况。即使有两个子句(通常是 和 选择),如果您无论如何都需要使用它,我发现点表示法很好。例如,我喜欢:
over
对于更复杂的查询(例如联接),我可能只是将两者分开:
我只是发现这种分离使其更易于阅读。
我相信,对于开发人员来说,至少了解查询表达式的基本功能非常有用 - 事实上,它们基本上是点表示法的翻译,而且语言本身对 LINQ to Objects、LINQ to SQL 等一无所知;这只是遵循适当模式的情况。这是一个很棒的设计,这意味着查询表达式只影响语言规范的一小部分。
They are equivalent. They don't just return the same result set - they compile to the same code.
Use query expressions or dot notation for individual cases depending on readability. For joins, I find the dot notation quite cumbersome - but I use it for situations where I only have one or two clauses (usually where/select). Even with two clauses (usually where and select) I find dot notation nice if you then need to use it anyway. For example, I like:
over
For more complicated queries (e.g. joins) I'd probably just separate the two:
I just find this separation makes it easier to read.
I believe it's really useful for developers to understand at least the basics of what query expressions do - the fact that they're basically translations into dot notation, and that the language itself doesn't know anything about LINQ to Objects, LINQ to SQL etc; it's just a case of following an appropriate pattern. It's a great bit of design, which means query expressions only affect one little bit of the language specification.
没有更好了。使用你喜欢的。在这种情况下,我会使用“查询”语法,因为我认为它比第二个语法更具可读性。此外,由于我们无论如何都在编写 SQL,所以我认为这种语法更类似于它。但我确信其他人会不同意并倾向于选择 lamda 版本。
顺便说一句,我通常使用后一种语法,因为我更喜欢 lambda 语法,因为它通常更易读且更短。
另请参阅这个问题。
There's no better one. Use what you like. I would, in this case, go with the 'query' syntax, since I think it's more readable than the second one. Moreover, since we're writing SQL anyway, I think this syntax resembles it better. But I'm sure others will disagree and tend to choose the lamda version.
I usually go with the latter syntax btw, since I prefer the lambda syntax cause it is often more readable and shorter.
See also this SO question.
我总是选择更具可读性的选项,在本例中我认为它是 LINQ 片段。
I'd always go for the more readable option and in this case I think its the LINQ snippet.
两者的性能都一样好,但我更喜欢第一个,因为它更容易阅读和理解。
Both will perform equally well, but I will prefer the first since it's easier to read and understand.
无论您觉得哪个更容易阅读。
Whichever you find easier to read.
“编写代码,然后对其进行测量以找出需要重构的内容。”两者你都测量过吗?
更好是主观的。
您是否发现其中任何一个存在问题,导致您想要选择其中一个而不是另一个?
"write your code, then measure it to find out what to refactor." Have you measured both?
Better is subjective.
Are you finding a problem with either one that is making you want to choose one over the other?
这两种语法之间有一点区别。查询表达式的编译时间将更长,因为编译器必须将其转换为点语法。当然,这几乎完全无关紧要,因为:
There IS one little difference between the two types of syntax. The query expression one will take longer to compile because the compiler has to transform it into the dot syntax. Of course, it is almost completely immaterial because:
当我必须动态构建查询时,我使用 Lambda 表达式;当查询完全已知时,我使用 LINQ to SQL。
I use Lambda Expressions when I have to build query dynamically, and LINQ to SQL when query is fully known.