选择LINQ包含另一个列表的对象列表

时间:2016-09-08 10:14:00

标签: c# linq linq-to-sql

我有三个数据库模型,如下所示

enter image description here

我有两个DTO类,如下所示

class RoleDTO
{
    string RoleId;
    string EnglishName;
    Guid TypeId;
    List<ClaimDTO> claims;
}

class ClaimDTO
{
    string ActionID;
    string ActionCode;
    string ActionLevel;
    string GrantDate;
}

现在我想从数据库中检索RoleDTO对象的列表。到目前为止我试过

public List<RoleDTO> GetRoleByType(Guid roleTypeId)
{
    var roleDTOs = (from r in ctx.Roles
          join rc in ctx.RoleClaims on r.RoleID equals rc.RoleID
          join a in ctx.Actions on rc.ActionID equals a.ActionID
          where r.RoleTypeID == roleTypeId
          select new RoleDTO
          {
              RoleId = r.RoleID,
              EnglishName = r.EnglishName,
              TypeId = r.TypeID,
              claims = List of ClaimDTO objects related to this role
          }).ToList();

    return roleDTOs;
}

我的问题是如何在select语句中检索ClaimDTO个对象的列表。我的linq是否正确? 我使用Telerik OpenAccess作为ORM

1 个答案:

答案 0 :(得分:0)

以下更改应有助于获得结果

public List<RoleDTO> GetRoleByType(Guid roleTypeId)
{
  var roleDTOs = (from r in ctx.Roles
  join rc in ctx.RoleClaims on r.RoleID equals rc.RoleID
  where r.RoleTypeID == roleTypeId
  select new RoleDTO
  {
      RoleId = r.RoleID,
      EnglishName = r.EnglishName,
      TypeId = r.TypeID,
      claims = ctx.Actions.Where( c => c.ActionId == rc.ActionId).Select( s => new ClaimDTO
      {
        ActionID = s.ActionID,
        ActionCode = s.ActionCode,
        ActionLevel = s.ActionLevel,
        GrantDate = s.GrantDate

      })ToList()


  }).ToList();

return roleDTOs;
}

另一种选择

List<ClaimDTO> claimsList = ctx.Actions.Select( s => new ClaimDTO
      {
        ActionID = s.ActionID,
        ActionCode = s.ActionCode,
        ActionLevel = s.ActionLevel,
        GrantDate = s.GrantDate

      })ToList();

var roleDTOs = ctx.Roles.Join(ctx.RoleClaims, r => r.RoleID, rc => rc.RoleID, (r,rc) => new 
            {
            r,rc
            }).Where( r => r.RoleTypeID == roleTypeId)
            .Select( row => new RoleDTO
            {
               RoleID = row.r.RoleID,
               EnglishName = row.r.EnglishName,
               TypeID = row.r.TypeID,
               claims = claimsList.Where( c => c.ActionId == rc.ActionId)
            }).ToList();

如果您使用Include方法,则以下查询可以提供帮助

 var roleDTOs = ctx.Roles.Include("RoleClaims").Join(ctx.Actions, r => r.RoleClaims.Select(rc => rc.ActionID).FirstOrDefault() , a => a.actionid , (r,a) => new
            {
              r,a
            }.Where(r => r.RoleTypeID == roleTypeId)
            .Select( row => new RoleDTO
            {
                 RoleID = row.r.RoleID,
                 EnglishName = row.r.EnglishName,
                 TypeId = row.r.RoleTypeID,
                 Claims = row.a.Select( c => new ClaimDTO
                 {
                    ActionID = c.ActionID,
                    ActionCode = c.ActionCode,
                    ActionLevel = c.ActionLabel,
                    GrantDate = row.r.RoleClaims.Select( g => g.grantDate)

                 })

            }).ToList();