Stefan Radulian
Stefan Radulian

Reputation: 1436

JayData one to many projections

I want to use jaydata JSLQ (JavaScript Language Query) to flatten a list of Post with one-to-many PostData into my ViewModel

My EF Entity looks like this:

public partial class Post
{       
    public Post()
    {           
        this.PostData = new HashSet<PostData>();           
    }

    public int Id { get; set; }

    public virtual ICollection<PostData> PostData { get; set; }
}

My database contains these records:

Table: Post
Id;...
1;...

Table: PostData
Id;PostId;FieldType; FieldValue
1; 1;     10;        "foo"
2; 1;     12;        "bar"
3; 1;     34;        "blah"

I want my view model in the client to look like this:

{id:1, title:'foo'}

That means, i want to put a filter on PostData that returns only FieldName==10, and i want to flatten that to a simple object.

How would I do that?

context.Posts.toArray(function(posts){console.dir(posts);})

returns an array of post objects. what next?

Upvotes: 0

Views: 528

Answers (1)

Peter Aron Zentai
Peter Aron Zentai

Reputation: 11750

This is achieved witht he SelectMany() function in EF and on this approach is not yet supported by JayData. You can however achieve the same output with directly querying aganst PostData and filtering on Post attributes.

context.PostDatas
  .filter( function(pd) { return pd.FieldType == 10 } )
  .map( function(pd) { return { 
                  PostID: pd.Post.Id, 
                  PostDataId: pd.Id, 
                  Title: pd.FieldValue }})
  .toArray( ... )

Upvotes: 1

Related Questions