Change syntax to Linq to SQL lambda join

2020-05-07 06:17发布

问题:

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

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

This is not working for me

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

Full query:

        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();

回答1:

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

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

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

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.



回答2:

I'm not sure it is really "worth" it as lambda joins to me are not pretty , something like this.

I was wrong this will crate a problem with Anonymous Type issue

DO NOT DO THIS

.Join (db.emp_info,  ru => ru.wwid, ei => ei.wwid, (ru, ei) => new { ru = ru, ei = ei })

INSTEAD DO THIS

.Join(Rpm_scrty_emp_info, z => z.Wwid, ei => ei.Wwid, (z, ei) => z)