【发布时间】:2012-12-03 21:27:21
【问题描述】:
似乎大多数 LINQ 都是用 lambda 表达式编写的。如何使用 lambda 重写这个 linq,有点混淆样式(尤其是连接)?
var responses =
from c in questionRepository.GetReponses()
join o in questionRepository.GetQuestions() on
c.QuestionID equals o.QuestionID
where c.UserID == 9999
orderby o.DisplayOrder
select new { o.QuestionText, c.AnswerValue };
【问题讨论】:
-
你真的不需要重写它。老实说,这很好。有时我使用一种风格,有时使用另一种风格,在这种情况下,我可能会选择查询语法。来自MSDN:
As a rule when you write LINQ queries, we recommend that you use query syntax whenever possible and method syntax whenever necessary. -
我喜欢这种 JOIN 表格。使用“Lambda 表达式”,它需要指定 4 个参数(加上接收者),虽然与上面相同,但看起来更混乱。
-
虽然查询语法更容易用于连接,但 lambda 语法更容易调试。详情见simple-talk.com/dotnet/.net-framework/…