1

I am trying to shape HTTP GET request where entity wall and some of its parent table(User) rows are displayed. I succeded getting the whole parent row, but I am struggling with select statement where I Want to choose which rows and values I want in my response.

[HttpGet]
        public async Task<ActionResult<IEnumerable<Wall>>> GetWalls()
        {

            return await _context.Walls
                .Include(p=>p.User)
                .ToListAsync();
        }

My response looks like this :

{
        "wallID": "46d7389f-2dcd-4b52-b4c0-32e7bbf5af24",
        "wallName": "erfnoerng",
        "dateCreated": "2020-07-31T11:53:21.9217551",
        "dateUpdated": null,
        "userID": "274eed93-fedf-4825-9c11-deb1e7678b99",
        "user": {
            "fullName": "Phil Hearing",
            "wall": [],
            "id": "274eed93-fedf-4825-9c11-deb1e7678b99",
            "userName": "philcuAdmin",
            "normalizedUserName": "PHILCUADMIN",
            "email": "phil@gmail.com",
            "normalizedEmail": "PHIL@GMAIL.COM",
            "emailConfirmed": false,
            "passwordHash": "AQAAAAEAACcQAAAAECPcBzMuCBrvo5jfSvRC8htiBMcXY6uHPymLvLLpqTOQc0iiYO9mB7XS82O0f2OEkA==",
            "securityStamp": "DCYN6LTUSAP3HSAD6ZEENPJ6CPZXCDAO",
            "concurrencyStamp": "fb06525c-0bad-4f06-bbdd-8f4404097a54",
            "phoneNumber": null,
            "phoneNumberConfirmed": false,
            "twoFactorEnabled": false,
            "lockoutEnd": null,
            "lockoutEnabled": true,
            "accessFailedCount": 0
        },
        "groupATerms": null,
        "groupBTerms": null,
        "groupCTerms": null,
        "groupDTerms": null,
        "groupAConnections": null,
        "groupBConnections": null,
        "groupCConnections": null,
        "groupDConnections": null
    }

I would like to get only Username, email and fullName from parent table but I get error when I write .Select statement which says that Select does not exist in current context.

fcuic
  • 35
  • 6
  • Where is select in your question? – vivek nuna Aug 01 '20 at 08:53
  • I was trying to add like in this post : https://stackoverflow.com/questions/3356541/entity-framework-linq-query-include-multiple-children-entities but it doesnt work. – fcuic Aug 01 '20 at 08:55
  • Try this .Include(x =>x.User).Include(y=>y.UserName) – vivek nuna Aug 01 '20 at 08:59
  • No, Lambda expression is not valid. These expressions are entity framework version of select queries, the other include does not recognize the first. – fcuic Aug 01 '20 at 09:05

1 Answers1

0

Create a ViewModel Like this:

public class DataViewModel
{
  public string FullName{ get; set; }
  public string Email{ get; set; }
  public string UserName{ get; set; }
}

And then:

[HttpGet]
public async Task<ActionResult> GetWalls()
{
    var result = await _context.Walls.Include(p=>p.User).Select(usr => new DataViewModel()
    {
        UserName = usr.User.UserName,
        Email= usr.User.Email,
        FullName= usr.User.FullName
    }).ToListAsync();

   return Ok(result);
}
hassan.ef
  • 1,169
  • 2
  • 10
  • 17
  • I am supposed to return IEnumerable, returning result in this form is not possible. – fcuic Aug 01 '20 at 11:17
  • you said that you would like to get only Username, email and fullName, then you can change your return like this `Task` and then `return Ok(result);`, why do you want to return `Task>>`? – hassan.ef Aug 02 '20 at 07:00
  • I dont, just like to check what I got as a response – fcuic Aug 02 '20 at 08:52