TCM
TCM

Reputation: 16900

exposing Associated entities in ria services

I have read this link :-

RIA Services: Entity Framework Reference Entities

and added [Include] in metadata class as well as in my domain service query.

On my client side i have a navigation property named Photos in my Albums entity. However it is of type EntityCollection and i don't know how to iterate over it. It has no get method and even indexing doesn't apply on it. I have tried the following :-

albums.photos[0]

but it doesn't work. Can anybody tell me how do i iterate over that photos collection in my albums entity ?

Thanks in advance :)

Upvotes: 0

Views: 339

Answers (1)

Rus
Rus

Reputation: 1827

So, in the metadata for your Albums class you have something like:

[MetadataTypeAttribute( typeof(Album.AlbumMetadata ) )]
public partial class Album
{
  internal sealed class AlbumMetadata
  {
    private AlbumMetadata ()
    { }

    [Include]
    public EntityCollection<Photo> Photos { get; set; } 
}

In your domain service you would have something like:

    public IEnumerable<Album> GetAlbums ()
    {
        var albums = from a in ObjectContext.Albums.Include( "Photos" )
                     orderby a.AlbumId descending
                     select a;
        return albums;
    }

In you clientside code you could then do:

public void LoadAlbumsWithPhotos ()
{
  LoadOperation<Album> albumLoader = Context.Load( Context.GetAlbumsQuery() );
  albumLoader.Completed += ( s, e ) =>
    {
       _albumStore = ( s as LoadOperation<Album> ).Entities.ToList();
    };
}

Photos could then be retrieved by:

var photos = _albumStore.First().Photos.Select( p => p);

Hope this helps.

Upvotes: 3

Related Questions