从查询转换为Lambda表达式

时间:2014-04-02 07:03:47

标签: c# sql linq lambda

我有以下查询根据需要提供输出:

var get = (from pg in _uow.PermissionGroups.Query()
           join pr in _uow.Permissions.Query() on pg.PermissionGroupId equals pr.PermissionGroupId
           join role in _uow.Roles.Query() on pr.Roles.Select(s => s.RoleId).FirstOrDefault() equals role.RoleId
           where pg.PermissionGroupName == "Services" && role.RoleId == 1
           select new { PermissionName = pr.PermissionName, Permission = true })
           .ToList<dynamic>();

我希望使用Join()在Lambda exression中执行此操作,但我遇到了各种异常:

var get2 = _uow.PermissionGroups.Query()
    .Join(
        _uow.Permissions.Query(),
        pgs => pgs.PermissionGroupId,
        prs => prs.PermissionGroupId,
        (pg, pr) => new 
        { 
            Permissions = pg.Permissons, 
            Roles = pr.Roles 
        })
    .Join(
        _uow.Roles.Query(),
        rs => rs.Roles.Select(e => e.RoleId).FirstOrDefault(),
        role => role.RoleId,
        (p, r) => new
        {
            PermissionName = p.Permissions.Select(d => d.PermissionName),
            PermissionGroupName = p.Permissions.Select(s => s.PermissionGroup.PermissionGroupName),
            Role = r
        })
    .Where(w => w.PermissionGroupName.FirstOrDefault() == "Services" && w.Role.RoleId == 1)
    .Select(c => new { PermissionName = c.PermissionName.SingleOrDefault(), Permission = true })
    .ToList<dynamic>();

任何建议都受到高度赞赏。

0 个答案:

没有答案