Get a List<T> from EntityQuery or EntitySet

2 posts, 1 answers
  1. Ala eddine
    Ala eddine avatar
    19 posts
    Member since:
    Feb 2012

    Posted 09 May 2012 Link to this post

    public

     

    public static IList<object> GenerateItems()
            {
     
                ModuleContext _context = new ModuleContext();
                EntityQuery<Fonctionnalite> q = _context.GetFonctionnalitesByModuleQuery(1);
                LoadOperation<Fonctionnalite> op = _context.Load(q);
               
                var result = new ObservableCollection<object>();
               
                foreach (var f in op.Entities.ToArray())
                {
                    var item = new MainPageViewModel();
                    item.Nom = f.Libelle;
                    item.id = f.idFonctionnalite;
                    foreach (var sf in f.SousFonctionnalites.ToArray())
                    {
                        var child = new MainPageViewModel();
                        child.id = sf.idSousFonctionnalite;
                        child.Nom = sf.Libelle;
                        item.RelatedItems.Add(child);
                    }
                    result.Add(item);
                }
                return result;
            }

     
    I want to display a Hierarchical Data in RadPanelBar form the entities Fonctionnalite and SousFoxtionnalite
    ( a Fonctionnalite has many SousFonctionnalite ) but i can't get a list from the Entityset<Fonctionnalite> !

  2. Answer
    Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 11 May 2012 Link to this post

    Hi Ala,

    This is probably a timing issue - when you start traversing the LoadOperation Entities collection, it's probably not yet generated. This is why it's better to implement your logic after the operation has completed. For that purpose you can handle the LoadOperation.Completed event:
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
     
            NorthwindContext _northwindContext = new NorthwindContext();
            EntityQuery<Customer> q = _northwindContext.GetCustomersQuery();
            _northwindContext.Load(q).Completed += new EventHandler(op_Completed);
        }
     
        public static IList<object> GenerateItems(LoadOperation<Customer> op)
        {
                
            var result = new ObservableCollection<object>();
     
            foreach (var cust in op.Entities.ToArray())
            {
                var item = new MainPageViewModel();
                item.Name = cust.CompanyName;
                item.Id = cust.CustomerID;
                foreach (var ord in cust.Orders.ToArray())
                {
                    var child = new MainPageViewModel();
                    child.Id = ord.OrderID.ToString();
                    child.Name = ord.ShipCountry;
                    item.RelatedItems.Add(child);
                }
                result.Add(item);
            }
            return result;
        }
     
        private void op_Completed(object sender, EventArgs e)
        {
            xPanelBar.ItemsSource = GenerateItems(sender as LoadOperation<Customer>);
        }

    I attached a sample solution demonstrating this approach. I hope it will get you started. Let us know if we can further assist you.

    Regards,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
Back to Top