如何使用带有lambda表达式的join重写此LINQ? [英] How to rewrite this LINQ using join with lambda expressions?

查看:119
本文介绍了如何使用带有lambda表达式的join重写此LINQ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎大多数LINQ都是用lambda表达式编写的.我该如何使用Lambda重写此linq,有点与样式(尤其是joins)混淆?

It seems like most LINQ is written with lambda expressions. How do I go about rewriting this linq using lambda, kinda confusion with the style (especially with joins)?

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语法",因为我认为它看起来更干净.

I prefer the "LINQ syntax" for Joins as I think it looks cleaner.

无论如何,这是将LINQ-join转换为"Lambda Expression" -join的方法.

In any case, here is how to translate the LINQ-join to the "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),它们只是与.链接在一起".-试试吧!

The other LINQ keywords are much simpler to convert (e.g. OrderBy(u => u.DisplayOrder) and are just "chained together" with .. - give it a go!

这篇关于如何使用带有lambda表达式的join重写此LINQ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆