Web Api: Return JSON with EF Included Member Field Count but not Member Object Data

asp.net-core-webapi entity-framework-core

Question

Scenario: Complaint object can have many Votes. In the GET request for All Complaints, I want to return the Vote Count, and not each individual vote object with the Complaint in the API response.

Here's the main classes:

//Model: Complaint.cs
public class Complaint
{
    public int Id { get; set; }
    public string Summary { get; set; }
    public List<Vote> Votes { get; set; }
    public int UpVoteCount=> Votes.Count(v => v.IsUpvote);
    public ApplicationUser Creator { get; set; }
}


//Model: Vote.cs
public class Vote
{
    public int Id { get; set; }
    public bool IsUpVote{ get; set; }
    public Complaint Complaint { get; set; }
    public ApplicationUser Creator { get; set; }
}


//DbContext: AppDbContext.cs
....
public IQueryable<Complaint> ComplaintsWithData =>
        Complaint
            .Include(complaint => complaint.Votes)
            .AsNoTracking();

//ApiController: ComplaintsController.cs
[HttpGet]
public IEnumerable<Complaint> GetComplaints()
{
      return _context.ComplaintsWithData.ToList();
}

In the current JSON response, I get the Vote count, however I also get each individual vote object's details as well (which I don't need in this call).

Current Response:

{
        "id": 2,
        "summary": "House was Stolen",
        "votes": [
            {
                "id": 146,
                "isUpvote": false,
                "creator": null
            },
                        {
                "id": 147,
                "isUpvote": false,
                "creator": null
            },
            ....
            ....
            ....
        ],
        "upVoteCount": 211,
    }

Desired Response:

{
        "id": 2,
        "summary": "House was Stolen",
        "upVoteCount": 211,
    }

I need to have the .Include(complaint => complaint.Votes) in the AppDbContext.cs file so that I can actually load the Votes to determine the Vote Count.

I do not want to store the Vote count as an actual database column.

Any advice would be greatly appreciated!

I am using .NET Core 2.0 Web API with Entity Framework Core.

Thanks in advance.

1
0
10/9/2017 10:11:15 AM

Popular Answer

There are several options. Below are two of them:

You might consider adding view models - classes with the exact set of properties you would like to return to UI. You just need to map your entity records to them.

Or just mark the properties you'd like to hide from being serialized to your JSON or XML endpoint response with NonSerialized attribute.

public class Complaint
{
    public int Id { get; set; }
    public string Summary { get; set; }
    [NonSerialized]
    public List<Vote> Votes { get; set; }
    public int UpVoteCount=> Votes.Count(v => v.IsUpvote);
    public ApplicationUser Creator { get; set; }
}
0
10/10/2017 6:38:33 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow