[英]outer Join in linq with lambda expression
我有2個表ProfileType(profilename),Role(roldId,minSecurityLevel),並且這兩個表之間的關系為roleProfiles(roleid,profilename,securityLevel)
我想為特定角色選擇所有profileType和該ProfileType的安全級別。
如果roleProfile不包含profiletype的條目,則將顯示null。
誰能僅使用lambda表達式來幫助我進行linq查詢?
我正在使用存儲庫模式
var roleprofiles = Repository.Query<Model.RoleProfile>()
.Select(
r =>
new RoleProfileModel
{
SecurityLevel = r.SecurityLevel,
ProfileCode = r.ProfileType.Code
})
.ToArray();
我想要在profileTypes上左聯接的結果
lambda版本
var roleprofiles = Repository.Query<Model.RoleProfile>()
.GroupJoin(Repository.Query<Model.ProfileType>(),
rp=>rp.Id, pt=>pt.TheRPId, // the the fields the tables are joined on
(rp,pt) => new RoleProfileModel
{
SecurityLevel = rp.SecurityLevel,
ProfileCode = rp.ProfileType.Code,
XYZ = pt.somethingFromProfType
})
.ToList(); // to array ??? to List is better
var details= (from p in repository.Query<Model.ProfileType>()
join r in repository.Query<Model.RoleProfile>() on p.Id equals r.ProfileTypeId into g
from x in g.DefaultIfEmpty()
select new RoleProfileModel{
ProfileType=p.Name,
SecurityLevel=x==null?string.Empty:x.SecurityLevel,
ProfileCode=x==null?string.Empty:x.Code,
}).ToArray();
希望對你有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.