简体   繁体   English

使用lambda表达式在linq中加入外部

[英]outer Join in linq with lambda expression

I have 2 tables ProfileType(profilename), Role(roldId,minSecurityLevel) and many to many relationship between this as roleProfiles(roleid,profilename,securityLevel) 我有2个表ProfileType(profilename),Role(roldId,minSecurityLevel),并且这两个表之间的关系为roleProfiles(roleid,profilename,securityLevel)

I want to select all profileTypes and security levels for that profileType to particular role. 我想为特定角色选择所有profileType和该ProfileType的安全级别。

If roleProfile does not contain the entry for profiletype it will show null. 如果roleProfile不包含profiletype的条目,则将显示null。

Can anyone help me for linq query using only lambda expression? 谁能仅使用lambda表达式来帮助我进行linq查询?

I am using repository Pattern 我正在使用存储库模式

        var roleprofiles =  Repository.Query<Model.RoleProfile>()
            .Select(
                r =>
                new RoleProfileModel
                {
                    SecurityLevel = r.SecurityLevel,
                    ProfileCode = r.ProfileType.Code
                })
            .ToArray();

I want the result for left join on profileTypes 我想要在profileTypes上左联接的结果

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

hope it will help you 希望对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM