Search code examples
devexpressxaf

DevExpress XAF: Get Collection of SubCollection


I have 3 Classes : GrandFother, Persons, Child

public class GrandFother: BaseObject
{
[Association("GrandFother_Persons")]
//......
public XPCollection<Persons > GFChilds
{
   get
    {
        return GetCollection<Persons >("GFChilds");
    }
}
}

public class Persons: BaseObject
{
[Association("Persons_Childs")]
// Other code ...
public XPCollection<Child> Childs
{
   get
    {
        return GetCollection<Child>("Childs");
    }
}
//Other code ...
}

public class Child: BaseObject
{
[Association("Persons_Childs")]
// Code ...
}

Now, what I want is that, in the class GrandFother, I want to get the list of all Childs that are associated to Persons that belongs to the grandfother

For example:

GrangFother1 has two Persons: Person1, Person2.  
Person1 has 2 childs: Per1Ch1, Per1Ch2.    
Person2 has 2 childs: Per2Ch1, Per2Ch2

And so, Add an XPCollection<Child> to the Class Grandfother that will contain: Per1Ch1, Per1Ch2, Per2Ch1, Per2Ch2, and if possible with sorting option.

Thanks.


Solution

  • You can use a [NonPersistent] collection property.

    [NonPersistent]
    public XPCollection<Child> GrandChildren
    {
        get
        {
            var result = new XPCollection<Child>(Session, GFChilds.SelectMany(x => x.Childs));
    
            // sorting
            SortingCollection sortCollection = new SortingCollection();
            sortCollection.Add(new SortProperty("Name", SortingDirection.Ascending));
            xpCollectionPerson.Sorting = sortCollection;
    
            return result;
        }
    }
    

    But you might not need an XPCollection<Child> - an IList<Child> will often do.

    [NonPersistent]
    public IList<Child> GrandChildren
    {
        get
        {
            return GFChilds
                      .SelectMany(x => x.Childs)
                      .OrderBy(x => x.Name);
        }
    }