我想将我的linq语句从查询语法切换到lambda,对我来说,最难理解的是lambda join
from ru in db.rpm_usr
join ei in db.emp_info on ru.wwid equals ei.wwid
所以上面的查询连接语法很简单,但是当我尝试将它放入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();
答案 0 :(得分:0)
我不确定它真的“值得”它因为lambda加入我并不漂亮,就像这样。
我错了,这会解决Anonymous Type issue
不要这样做
.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)
答案 1 :(得分:0)
我个人发现通过将查询分解为单独的行更容易更好地理解点符号中的LINQ。
继上述@ Tom的示例之后,查询按如下方式分解:
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的第一个示例,并利用匿名对象返回连接的两侧,甚至是特定属性。
如果您还没有,我强烈推荐使用LINQpad软件。它非常适合调试LINQ,还有一个用于在查询和lambda语法之间切换的内置转换器。