0

I am writing a simple community Plugin and now I wanted to improve my db queries but run into an blocking isse:

this is the query:

TRDCommunityViewModel community = await _db.Communities
  .Select(c => new TRDCommunityViewModel
   {
    Id = c.Id,
    CommunityId = c.Guid,
    Created = c.Created,
    Description = c.Description,
    IAmAdmin = (c.Admins.FirstOrDefault(k => k.Id == userId) != null),
    Members = c.Members.Select(a => new List<TRDIdenityViewModel>()
      {
        // member vars of TRDIdenityViewModel arn't accessible
        // ???
      }),
    Posts = c.Posts.Select(a => new List<TRDIdenityViewModel>()
      {
        //member vars of TRDIdenityViewModel arn't accessible
      })
        // and so on
    })
   .FirstOrDefaultAsync(k => k.Id == Id);

My question is...how is it possible to query for related lists? Members and Posts are ICollections

Thx for helping.

1 Answer 1

1

You should not need to instantiate a List within the Select, this should work:

TRDCommunityViewModel community = await _db.Communities
  .Select(c => new TRDCommunityViewModel
   {
    Id = c.Id,
    CommunityId = c.Guid,
    Created = c.Created,
    Description = c.Description,
    IAmAdmin = (c.Admins.FirstOrDefault(k => k.Id == userId) != null),
    Members = c.Members.Select(a => new TRDIdenityViewModel()
      {
        // do your assignments here...
      }),
    Posts = c.Posts.Select(a => new TRDIdenityViewModel()
      {
        //do your assignments here...
      })
        // and so on
    })
   .FirstOrDefaultAsync(k => k.Id == Id);

If you need to you can materialize by calling ToList() after select, e.g.:

    Posts = c.Posts.Select(a => new TRDIdenityViewModel()
      {
        //do your assignments here...
      }).ToList()
Sign up to request clarification or add additional context in comments.

1 Comment

Ufff...so simple. Thx alot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.