c# – 使用LINQ Entity-Framework进行LEFT JOIN或RIGHT JOIN
发布时间:2020-12-16 01:54:53 所属栏目:百科 来源:网络整理
导读:尝试使用 Linq加入. 我该怎么用?左连接还是右连接? APPLICANT TABLE PROFILE TABLEAPPLICANT_ID|profile_id|Applicant_Name| |profile_id|Applicant_Name 1 | NULL | RAY HEAVENS | | 1 | MARK LAPID 2 | NULL | BEN TULFO | | 2 | SUPER MAN 3 | 1 | NULL
尝试使用
Linq加入.
我该怎么用?左连接还是右连接? APPLICANT TABLE PROFILE TABLE APPLICANT_ID|profile_id|Applicant_Name| |profile_id|Applicant_Name 1 | NULL | RAY HEAVENS | | 1 | MARK LAPID 2 | NULL | BEN TULFO | | 2 | SUPER MAN 3 | 1 | NULL | | 3 | BRANDON KNIGHT 4 | 2 | NULL | | 5 | 3 | NULL | | DESIRED OUTPUT: APPLICANT_ID | Applicant_Name 1 | RAY HEAVENS 2 | BEN TULFO 3 | MARK LAPID 4 | SUPERMAN 5 | BRANDON KNIGHT 这是我的控制器中的代码: var applicantList = (from a in context.Profiles join app in context.APPLICANTs on a.PROFILE_ID equals app.Profile_id into output from j in output.DefaultIfEmpty(new APPLICANT()) select j ).Take(1000).AsEnumerable(); applicantdata = applicantList.AsQueryable().OrderBy(v => v.APPLICANT_ID).ToList(); if (applicantdata.Any()) { Cache.Set("applicants",applicantdata,30); } } return applicantdata; } 我希望有人可以推荐我使用或做什么. 解决方法
针对错误进行了更新:您需要创建具有所需属性的新类并将其返回.
var applicantList = (from app in context.APPLICANTs join a in context.Profiles on app.Profile_id equals a.PROFILE_ID into output from j in output.DefaultIfEmpty() select new { APPLICANT_ID = app.APPLICANT_ID,Applicant_Name = (j == null ? app.Applicant_Name : j.Applicant_Name) }).Take(1000).AsEnumerable(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |