2009-06-03 8 views
0

Kann man in LINQ nach mehr als einer Gruppe gruppieren?LINQ zum Zurückziehen des Objektdiagramms

Zum Beispiel, ich habe diese Abfrage (offensichtlich unvollständig)

from lxr in LOCATION_XREFs 
    join l in LOCATIONs on lxr.LOCATION_SKEY equals l.LOCATION_SKEY 
    join c in COMPANies on l.COMPANY_SKEY equals c.COMPANY_SKEY 
    join prlx in PeopleRoleLocationXrefs on lxr.LOCATION_XREF_SKEY equals prlx.LOCATION_XREF_SKEY 
    join p in PEOPLEs on prlx.PEOPLE_SKEY equals p.PEOPLE_SKEY 
    join pr in PeopleRoles on prlx.PeopleRoleKey equals pr.PeopleRoleKey 

... und ich möchte wieder eine Reihe von Unternehmen erhalten, die eine Reihe von Standorten, die wiederum haben eine Gruppe von Leuten.

+0

Sind Sie versuchen, zu einer Gruppe ... oder haben Sie ein Objekt bauen wollen Graph? Die Gruppierung gibt eine flache Ergebnismenge zurück ... aber basierend auf Ihrer Frage klingt es so, als ob Sie ein Objektdiagramm wünschen. – jrista

+0

Sie haben Recht, ich möchte ein Objektdiagramm. Bitte erläutern Sie den Unterschied und ich passe meinen Beitrag entsprechend an (oder irgendjemand kann dies tun, da ich es als "Community-Wiki" gemacht habe) - Danke, –

Antwort

1

Wenn Sie zu einer Gruppe von mehreren Parametern suchen, ist das möglich:

from lxr in LOCATION_XREFs 
    join l in LOCATIONs on lxr.LOCATION_SKEY equals l.LOCATION_SKEY 
    join c in COMPANies on l.COMPANY_SKEY equals c.COMPANY_SKEY 
    join prlx in PeopleRoleLocationXrefs on lxr.LOCATION_XREF_SKEY equals prlx.LOCATION_XREF_SKEY 
    join p in PEOPLEs on prlx.PEOPLE_SKEY equals p.PEOPLE_SKEY 
    join pr in PeopleRoles on prlx.PeopleRoleKey equals pr.PeopleRoleKey 
group c by new { c.COMPANY_SKEY, l.LOCATION_SKEY} into myGroup 
select new 
{ 
    myGroup.Key.COMPANY_SKEY, 
    myGroup.Key.LOCATION_SKEY, 
    myGroup.Count() 
} 
+0

. Siehe meinen Kommentar zu jrista. Ich suche nach einem Objektgraphen. Entschuldigung, meine Frage war aufgrund meines Missverständnisses irreführend. Davon abgesehen denke ich, dass ich diese Frage hätte kopieren können: http://stackoverflow.com/questions/740063/loading-complete-graph-from-sql-using-linq –

0

habe ich schließlich den folgenden Code zu erreichen, was ich wollte. Interessanter Hinweis: Beachten Sie, dass die Rollen flach und mit Kommas verkettet werden sollten. Daher habe ich einen Wrapper-Accessor verwendet, der den Aggregatoperator verwendet. Anscheinend ist der Aggregate Operator innerhalb einer LINQ-Abfrage nicht suuported, wie Sie bekommen "the query operator "Aggregate" is not supported" sonst würde ich es genau dort ähnliche Roles = new List<string>(g.Select(u => u.pr.RoleName)).Aggregate((a, b) => a + ", " + b) Inline getan haben

public class DAOPerson 
{ 
    public string Name { get; set; } 
    public int PeopleSkey { get; set; } 
    public List<string> RolesCollection { get; set; } 
    public string Roles 
    { 
     get { return RolesCollection.Aggregate((a, b) => a + ", " + b); } 
    } 
} 

IQueryable<DAOLocation> gridData; 
gridData = (from lxr in db.LOCATION_XREFs 
      join l in db.LOCATIONs on lxr.LOCATION_SKEY equals l.LOCATION_SKEY 
      join c in db.COMPANies on l.COMPANY_SKEY equals c.COMPANY_SKEY 
      where lxr.DEPARTMENT_NUMBER == Department.BINDING_AUTHORITY_KEY 
        && lxr.BRANCH_NUMBER == Branch.ATLANTAKEY 
      orderby c.NAME, l.INTERNAL_NAME ascending 
      select new DAOLocation 
         { 
          CompanyName = c.NAME, 
          CompanySkey = c.COMPANY_SKEY, 
          LocationName = l.INTERNAL_NAME, 
          LocationSkey = l.LOCATION_SKEY, 
          Persons = (from prlx in db.PeopleRoleLocationXrefs 
             join lxr2 in db.LOCATION_XREFs on prlx.LOCATION_XREF_SKEY equals lxr.LOCATION_XREF_SKEY 
             join p in db.PEOPLEs on prlx.PEOPLE_SKEY equals p.PEOPLE_SKEY 
             join pr in db.PeopleRoles on prlx.PeopleRoleKey equals pr.PeopleRoleKey 
             where lxr2.LOCATION_SKEY == l.LOCATION_SKEY 
             group new { p, pr } by p.PEOPLE_SKEY into g 
             select new DAOPerson 
               { 
                Name = g.First().p.LAST_NAME, 
                PeopleSkey = g.First().p.PEOPLE_SKEY, 
                RolesCollection = new List<string>(g.Select(u => u.pr.RoleName)) 
               }).ToList() 
         });