开发者

How to auto-load details (with conditions) associated with an entity using Ria Services?

I'm developing a project using Silverlight 4 and Entity Framework 4 and I'm trying to auto-load the details (with conditions) associat开发者_JAVA技巧ed with an entity when the client loads the EntityQuery.

So far, I've been able to put in place a solution, using the Include attribute, that returns all the details associated with the master entity. What I'm missing here is to be able to filter out the details based on some criteria.

As an example, here's what my entities look like:

Entity Movie

Id (int)

[Include]
MovieLocalizedInformations (EntityCollection<MovieLocalizedInformation>)

Entity MovieLocalizedInformation

Id (int)
Movie_Id (int)
LanguageCode (eg.: en)
Title

On my DomainService object, I expose the following method:

public IQueryable<Movie> GetMovies( string languageCode )
{
  return this.ObjectContext.Movies.Include( "MovieLocalizedInformations" );
}

This works fine. But when I try to add where clause to filter out the localized information based on the language code, only the movies get loaded on the client.

Is there a way to achieve the filtering in one query?

Note: I'm also using the DomainDataSource with paging on the client so the solution needs to work with that.

Any help would be greatly appreciated!

Thanks,

Jacques.


Not sure about Enitity Framework but with a LinqToSqlDomainService you use the LoadWith loadOption to include the details entities and then use the AssociateWith LoadOption to filter the detail e.g

DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Movies>(i => i.MovieLocalizedInformations);
options.AssociateWith<Movies>(i => i.MovieLocalizedInformations.Where(d=> myListOfIds.Contains(d.LocationId)));


Ok,

For efficiency reason, I decided to go with custom DTO object that fetches the localized information and flatten the result.

But, the same problem occurred when my custom DTO needed to reference another custom localized DTO.

Here is how I came to do the same as the .Include( "PropertyName" ) that the ObjectSet offers:

Entity LocalizedMovieCollection

public class LocalizedMovieCollection
{
  [Key]
  public int Id { get; set; }
  public string Name { get; set; } (the result of a sub query based on the language)

  [Include]
  [Association( "LocalizedMovieCollection_LocalizedMovies", "Id", "MovieCollection_Id" )]
  public IEnumerable<LocalizedMovie> Movies { get; set; }
}

Entity LocalizedMovie

public class LocalizedMovie
{
  [Key]
  public int Id { get; set; }
  public string Name { get; set; } (the result of a sub query based on the language)
  public int MovieCollection_Id { get; set; }

  [Include]
  [Association( "LocalizedMovie_LocalizedMovieCollection", "MovieCollection_Id", "Id", IsForeignKey = true]
  public LocalizedMovieCollection MovieCollection { get; set; }
}

Then, I've declared two methods: One that returns an IQueryable of LocalizedMovieCollection and the other, an IQueryable of LocalizedMovie. (Note: There must be at least one method that returns each type of entity for the entity to get auto-generated on the Silverlight client)

My goal is to automatically load the MovieCollection associated with a Movie so the method definition to get the movies is as follow:

public IQueryable<LocalizedMovie> GetMovies( string languageCode )
{
  return from movie in this.ObjectContext.Movies
         join movieLocalizedInfo in this.ObjectContext.MovieLocalizedInformations
           on movie equals movieLocalizedInfo.Movie
         join movieCollection in this.ObjectContext.MovieCollections
           on movie.MovieCollection equals movieCollection
         join movieCollectionLocalizedInfo in this.ObjectContext.MovieCollectionLocalizedInformations
           on movieCollection equals movieCollectionLocalizedInfo.MovieCollection
         where movieLocalizedInfo.LanguageCode == languageCode && movieCollectionLocalizedInfo.LanguageCode == languageCode
         select new LocalizedMovie()
           {
             Id = movie.Id,
             Name = movieLocalizedInfo.Name
             MovieCollection_Id = movieCollection.Id,
             MovieCollection = new LocalizedMovieCollection(){ Id = movieCollection.Id, Name = movieCollectionLocalizedInfo.Name }
           }
}

When the Silverlight client loads the query, all the LocalizedMovies and their associated LocalizedMovieCollections will be loaded into the context.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜