2

How do I get an object from context with a nested collection as an id list?

I want to get a Flat object containing a list of user IDs.

public IEnumerable<Flat> GetAll()
{
    return _context.Flats
        .Include(flat => flat.Users.Select(x => x.Id));
}

The error returned:

The property expression 'flat => {from User x in flat.Users select [x].Id}' is not valid. The expression should represent a property access: 't => t.MyProperty'.

2
  • Is this EntityFramework?
    – trailmax
    Sep 19, 2018 at 13:50
  • Not sure what you are trying to do. You want all Flats objects also get list of IDs from users associated with each flat?
    – trailmax
    Sep 19, 2018 at 13:51

1 Answer 1

5

This should be one way to do it:

public IEnumerable<dynamic> GetAll()
{
    return _context.Flats
        .Include(flat => flat.Users)
        .Select(flat => new { Flat = flat, UserIds = flat.Users.Select(u => u.Id) });
}

Although you probably want to define a new type to return.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.