DevExpress XAF: получение коллекции подколлекций

У меня есть 3 класса: дедушка, люди, ребенок

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

Теперь я хочу, чтобы в классе GrandFother я хотел получить список всех дочерних элементов, связанных с лицами, принадлежащими дедушке.

Например:

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

Итак, добавьте XPCollection<Child> к дедушке класса, который будет содержать: Per1Ch1, Per1Ch2, Per2Ch1, Per2Ch2 и, если возможно, с опцией сортировки.

Спасибо.


person Amine Mestiri    schedule 17.12.2018    source источник
comment
Я отредактировал ваш вопрос, улучшив либо его форматирование, либо ее качество, чтобы помочь людям понять ваш вопрос и помочь вам получить правильный ответ. Но вам все равно может потребоваться добавить дополнительную информацию, чтобы ваш вопрос стал полностью решаемым.   -  person Bsquare ℬℬ    schedule 17.12.2018


Ответы (1)


Вы можете использовать свойство коллекции [NonPersistent].

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

Но вам может не понадобиться XPCollection<Child> — часто достаточно IList<Child>.

[NonPersistent]
public IList<Child> GrandChildren
{
    get
    {
        return GFChilds
                  .SelectMany(x => x.Childs)
                  .OrderBy(x => x.Name);
    }
}
person shamp00    schedule 19.12.2018
comment
Огромное спасибо. Я использовал XPCollection. IList сделал исключение приведения объектов. - person Amine Mestiri; 20.12.2018