将语法更改为Linq到SQL Lambda Join [英] Change syntax to Linq to SQL lambda join

查看:62
本文介绍了将语法更改为Linq到SQL Lambda Join的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将linq语句从查询语法切换为lambda,对我来说,我最难理解的部分是lambda join

I want to switch my linq statement from query syntax to lambda, for me the hardest part to me to understand is the lambda join

 from ru in db.rpm_usr
    join ei in db.emp_info on ru.wwid equals ei.wwid

因此,上面的查询联接语法很简单,但是当我尝试将其放入lambda时

So above that query join syntax is easy but when I try to put it into lambda

这对我不起作用

  .Join(Rpm_scrty_emp_info, p => p.Iact_ind, j => j.Wwid

完整查询:

        var queryAllUsers = (from ru in db.rpm_usr
                                  join ei in db.emp_info on ru.wwid equals ei.wwid
                                  let cdis_eml = ei.dmn_addr + ";"
                                  where ru.inact_ind == "N" && ei.inact_ind == "N" && ei.dmn_addr != null
                                  orderby ei.dmn_addr
                                  select new rpm_scrty_rpm_usr()
                                  {

                                      usr_id = ru.usr_id,
                                      usr_lnm = ru.usr_lnm,
                                      usr_pwd = ru.usr_pwd,
                                      usr_fnm = ru.usr_fnm,
                                      wwid = ru.wwid,
                                      apprvr_wwid = ru.apprvr_wwid,
                                      chg_dtm = ru.chg_dtm,
                                      chg_usr_id = ru.chg_usr_id,
                                      dflt_ste_id = ru.dflt_ste_id,
                                      cre_dtm = ru.cre_dtm,
                                      cre_usr_id = ru.cre_usr_id,
                                      lst_pwd_chg_dtm = ru.lst_pwd_chg_dtm,
                                      lst_accs_dtm = ru.lst_accs_dtm,
                                      email_id = ru.email_id,
                                      inact_ind = ru.inact_ind,
                                      salt = ru.salt,
                                      tel = ru.tel
                                  }).ToList();

推荐答案

我个人发现,通过将查询分解为单独的行,可以更轻松地更好地理解点表示法中的LINQ.

I personally find it easier to better understand LINQ in dot notation by breaking down the query onto separate lines.

根据上面@Tom的示例,查询细分如下:

Following on from @Tom's example above, the query breaks down as follows:

user.Join(Rpm_scrty_emp_info,  // We want to join user to Rpm_scrty_emp_info
      z => z.Wwid,             // On the left "user" side of the join we want to use the Wwid property
      ei => ei.Wwid,           // On the right "employee info" side of the join we want to use the Wwid property 
      (z, ei) => z)            // We then want to return the results on the left side of the join, i.e. our z objects

您还可以遵循@Tom的第一个示例,并利用匿名对象返回联接的双方,甚至返回特定的属性.

You can also follow @Tom's first example and leverage anonymous objects to return both sides of the join, or even specific properties.

如果您还没有的话,我强烈建议您使用LINQpad软件.这对于调试LINQ非常有用,并且具有内置的转换器,可在查询和lambda语法之间进行切换.

If you don't have it already, I'd highly recommend the software, LINQpad. It's great for debugging LINQ and also has a built-in converter for switching between query and lambda syntax.

这篇关于将语法更改为Linq到SQL Lambda Join的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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