看起来大多数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 };
解决方法
我更喜欢Joins的“LINQ语法”,因为我认为它看起来更干净.
无论如何,这里是如何将LINQ-join转换为“Lambda Expression”-join.
翻译为:
- from a in AA
- join b in BB on
- a.Y equals b.Y
- select new {a,b}
方法是:
- AA.Join( // L
- BB,// R
- a => a.Y,b => b.Y,// L -> join value,R -> join value
- (a,b) => new {a,b}) // L+R result
其他LINQ关键字转换起来要简单得多(例如OrderBy(u => u.DisplayOrder),只是与…链接在一起) – 给它一个去吧!