2016-09-08 3 views
1

Ich habe drei Datenbank-Modell, das unterWählen Sie Liste des Objekts, das eine andere Liste von LINQ enthält

enter image description here

Ich habe zwei DTO Klasse gezeigt, die unter

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

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

Jetzt angezeigt Ich mag Abrufen der Liste von RoleDTO Objekt aus der Datenbank. Bisher habe ich versucht,

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; 
} 

Meine Frage ist, wie kann ich Liste der ClaimDTO Objekte in Select-Anweisung abzurufen. Ist meine linq korrekt? Ich verwende Telerik OpenAccess als ORM.

+0

Ansprüche = r.claims. Wählen Sie (x => HIER MEHR CODE EINGEBEN) – jdweng

Antwort

0

Below Änderung soll dazu beitragen, die Ergebnisse

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; 
} 

Eine weitere Alternative

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

zu bekommen Wenn Sie Methode verwenden Fügen Sie dann unter Abfrage

helfen könnte
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(); 
+0

Ansprüche ist eine Liste von Objekten. –

+0

Bearbeitet jetzt zum Abrufen aus der Liste –

+0

Dies gibt Ausnahme beim Konvertieren von LINQ nach SQL –

Verwandte Themen