Print Page | Close Window

Include and problem of RelatedEntity<T>

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3467
Printed Date: 13-May-2026 at 6:26am


Topic: Include and problem of RelatedEntity<T>
Posted By: pponzano
Subject: Include and problem of RelatedEntity<T>
Date Posted: 31-May-2012 at 8:52am
Hello,
I premit I'm fair new of Ideablade....
I'm tring to fetch data from a silverlight application with mvvm (cocktail),I've this kind of relation

Dashboard (*) <-> (*) Indicatori (1) -> (*) SNAPSHOT

I'm able to run this query and have the Snapshot taken with include

public OperationResult<IEnumerable<INDICATORI>> GetIndicatoriFromDashboard(int idDashboard, Action<IEnumerable> onSuccess = null, Action<Exception> onFail = null)

       {

           var q = entityManagerProvider.Manager.INDICATORI.Select(o1 => o1).Include(o1=>o1.SNAPSHOT);

 

           EntityQueryOperation<INDICATORI> op = q.ExecuteAsync();

 

           return op.OnComplete(onSuccess, onFail).AsOperationResult<INDICATORI>();

       }


I'm not able to filter on idDashboard since if I do

public OperationResult<IEnumerable<RelatedEntityList<INDICATORI>>> GetIndicatoriFromDashboard(int idDashboard, Action<IEnumerable> onSuccess = null, Action<Exception> onFail = null)

      {

          var query = entityManagerProvider.Manager.DBOARD.Where(o1 => o1.ID == idDashboard).Select(o1 => o1.INDICATORI).Include("SNAPSHOT");

          var q = entityManagerProvider.Manager.INDICATORI.Select(o1 => o1).Include(o1 => o1.SNAPSHOT);

 

          var op = query.ExecuteAsync();

 

 

          return op.OnComplete(onSuccess, onFail).AsOperationResult();

      }


Executing this I got The result type of the query is neither an EntityType nor a CollectionType with an entity element type. An Include path can only be specified for a query with one of these result types.

I've read that include cannot be filtered....but how can I filter from idDashboard? if I were in wpf I would have done a ToList and then split but in sl I must do it async......any help?
Thanks




Replies:
Posted By: mgood
Date Posted: 31-May-2012 at 10:36am
Ok, so you want to query INDICATORI entities and not DBOARD entities. So, first of all your query should start with INDICATORI, because that's what you want to return. Based on your relationships, INDICATORI should have a DashboardID property. Not sure what it's called in your model.
 
So, the following should work, assuming the names are correct. A little tip on the side, make a habbit of appending Async at the end of asynchronous methods. Makes it clear to the caller that the method follows an asynchronous calling pattern.
 

public OperationResult<IEnumerable<INDICATORI>> GetIndicatoriFromDashboardAsync(int idDashboard, Action<IEnumerable<INDICATORI>> onSuccess = null, Action<Exception> onFail = null)

      {

          var query = entityManagerProvider.Manager.INDICATORI

                     .Include(o1 => o1.SNAPSHOT)
                     .Where(x => x.DashboardID == idDashboard);

 

          var op = query.ExecuteAsync();

 

          return op.OnComplete(onSuccess, onFail).AsOperationResult();

      }


 
 


Posted By: mgood
Date Posted: 31-May-2012 at 10:42am
Oh, sorry, didn't notice you had a many-to-many there. So, let's try again. INDICATORI should have a Dashboards related entity list, again not sure what you called the property.
 

public OperationResult<IEnumerable<INDICATORI>> GetIndicatoriFromDashboardAsync(int idDashboard, Action<IEnumerable<INDICATORI>> onSuccess = null, Action<Exception> onFail = null)

      {

          var query = entityManagerProvider.Manager.INDICATORI

                     .Include(o1 => o1.SNAPSHOT)
                     .Where(x => x.Dashboards.Any(y => y.ID == idDashboard));

 

          var op = query.ExecuteAsync();

 

          return op.OnComplete(onSuccess, onFail).AsOperationResult();

      }




Posted By: mgood
Date Posted: 31-May-2012 at 11:12am
Reading your post again, I'm not sure if you need INDICATORI entities or if you only want the SNAPSHOP entities. If you only want the snapshot entities, then you can do the following:
 

public OperationResult<IEnumerable<SNAPSHOT>> GetSnapshotFromDashboardAsync(int idDashboard, Action<IEnumerable<SNAPSHOT>> onSuccess = null, Action<Exception> onFail = null)

      {

          var query = entityManagerProvider.Manager.INDICATORI

                     .Where(x => x.Dashboards.Any(y => y.ID == idDashboard))
                     .SelectMany(x => x.SNAPSHOT)

 

          var op = query.ExecuteAsync();

 

          return op.OnComplete(onSuccess, onFail).AsOperationResult();

      }

or if you want the SNAPSHOT entities, but also get each associated INDICATORI with it, then you invert the query and start with SNAPSHOT:
 

public OperationResult<IEnumerable<SNAPSHOT>> GetSnapshotFromDashboardAsync(int idDashboard, Action<IEnumerable<SNAPSHOT>> onSuccess = null, Action<Exception> onFail = null)

      {

          var query = entityManagerProvider.Manager.SNAPSHOT

                     .Include(x => x.INDICATORI)
                     .Where(x => x.INDICATOR.Dashboards.Any(y => y.ID == idDashboard));

 

          var op = query.ExecuteAsync();

 

          return op.OnComplete(onSuccess, onFail).AsOperationResult();

      }



Posted By: pponzano
Date Posted: 01-Jun-2012 at 8:20am
Hello Marcel,

var query = entityManagerProvider.Manager.INDICATORI
                     .Include(o1 => o1.SNAPSHOT)
                     .Where(x => x.Dashboards.Any(y => y.ID == idDashboard));
was exacly what I was looking for...I wasn't aware of the any usage in this way so I woudn't succed in!

Thanks a lot!






Print Page | Close Window