Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
Is there any way I can achieve this in Entity Framework?

I'm running an ASP.NET Core Web API project and I have this model class with a self-referencing association :

public class VehicleCategory 
{
    [Key]
    public int Id { get; set; }
    public string description { get; set; }

    [ForeignKey("ParentVehicleCategoryId")]
    public int? ParentVehicleCategoryId { get; set; }
    [JsonIgnore]
    public virtual VehicleCategory ParentVehicleCategory { get; set; }
    public virtual IEnumerable<VehicleCategory> subVehicleCategories { get; set; }
}

I have configured everything in EF to retrieve get all data and added this config into the startup.cs file to prevent looping:

services.AddControllers().AddNewtonsoftJson(options =>
            options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );

The problem is that when I try to get all data from the database, I end up with this json result :

[
  {
    "id": 7,
    "description": "description 1",
    "parentVehicleCategoryId": null,
    "subVehicleCategories": [
      {
        "id": 9,
        "description": "description 2",
        "parentVehicleCategoryId": 7,
        "subVehicleCategories": null
      },
      {
       "id": 10,
        "description": "description 3",
        "parentVehicleCategoryId": 7,
        "subVehicleCategories": null
      }
    ]
  },
  {
    "id": 9,
    "description": "description 2",
    "parentVehicleCategoryId": 7,
    "subVehicleCategories": null
  },
  {
    "id": 10,
    "description": "description 3",
    "parentVehicleCategoryId": 7,
    "subVehicleCategories": null
  }
]

Is there a way I can only have this data with only the array representing the children:

[
  {
    "id": 7,
    "description": "description 1",
    "parentVehicleCategoryId": null,
    "subVehicleCategories": [
      {
        "id": 9,
        "description": "description 2",
        "parentVehicleCategoryId": 7,
        "subVehicleCategories": null
      },
      {
       "id": 10,
        "description": "description 3",
        "parentVehicleCategoryId": 7,
        "subVehicleCategories": null
      }
    ]
  },
 
]

To get data I'm using this code:

var test = _raceContext.VehicleCategories
                       .Include(x => x.subVehicleCategories)
                       .ToList();
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If you want to include only top-level entities, and their children listed inside, you could use a Where clause to get only those where ParentVehicleCategory is null:

var test = _raceContext.VehicleCategories
                       .Where(vc => vc.ParentVehicleCategory == null)
                       .Include(x => x.subVehicleCategories)
                       .ToList();

This way, the top-level list is the "parent-less" entities, but the second-level entities will appear in your tree, because those entities are all children of some parent, and is included because of your call to the Include method. If you have only two levels of hierarchy, this method will work for you, but if a "child" entity can also be a "parent", you will need to do some extra work.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!