Я пытаюсь отсортировать дочернюю коллекцию с помощью Entity Framework.Это мой код:
var query = db.Category
.Where(p => p.parrent_id == null)
.OrderByDescending(x => x.prefix)
.Select(o => new
{
Category = o,
SubCategories = o.Category1.OrderBy(h => h.prefix)
});
IEnumerable<Category> cats = query.AsEnumerable()
.Select(x => new Category
{
category_id = x.Category.category_id,
parrent_id = x.Category.parrent_id,
category_name = x.Category.category_name,
prefix = x.Category.prefix,
Category1 = x.SubCategories.ToEntityCollection()
});
метод ToEntityCollection выглядит следующим образом:
public static EntityCollection<T> ToEntityCollection<T>(this IEnumerable<T> source) where T : class
{
var es = new EntityCollection<T>();
foreach (T e in source)
{
es.Add(e);
}
return es;
}
Я получаю следующую ошибку:
System.InvalidOperationException: The object could not be added to the EntityCollection or EntityReference. An object that is attached to an ObjectContext cannot be added to an EntityCollection or EntityReference that is not associated with a source object.
on
es.Add(e);
Заранее спасибо!