-1

I'm new on serializing Json files and i don't really know how to figure out my problem. My Db structure is made like this: A single Structure is related to multiple Units, and a single Unit to multiple Media, Details and Prices; So i have a structure like this:

List<Units> Units = Db.Units.Where(o => o.StructureId == StructureId).ToList();

        foreach (Unit uni in Unita)
        {
            /* Getting units pics */
            List<UnitMedia> Media = Db.UnitMedia.Where(o => o.UnitId == uni.UnitId).ToList();

            /* Getting units details */
            List<UnitDetails> Details = Db.UnitDetails.Where(o => o.UnitId == uni.UnitId).ToList();

            /* Getting units prices */
            List<UnitPrices> Prices = Db.UnitPrices.Where(o => o.UnitId == uni.UnitId).ToList();
        }

Now what i want to do is to create a Json structure like:

Unit
{
  SomeUnitAttributes

  UnitMedia
  {
    UnitMediaDetails
  }
  UnitDetails
  {
    UnitDetailsDetails
  }
  UnitPrices
  {
    UnitPricesDetails
  }
}

This must be the return of an API. Thank you all for your help

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Raiaki
  • 1
  • Are you going to throw your db entities into that? (I wouldn't recommend it; usually better to have a set of classes you copy some of the db data into before you serialize to the front end ) – Caius Jard Mar 14 '22 at 20:21
  • Incidentally if this is EF you can save yourself some headache with that manual approach you have there by just using Include to download additional data if your entities have navigation props eg `Db.Units.Include(u => u.UnitPrices).Include(u => u.UnitDetails).Include(u => u.UnitMedia).Where(o => o.StructureId == StructureId).ToList();` – Caius Jard Mar 14 '22 at 20:23

1 Answers1

0

Usually it's best to define separate model classes for objects you're planning to serialize, rather than using the Entity Framework models. These are frequently called "DTOs" or "Data Transfer Objects." You might populate them like this:

var unitDtos = Db.Units.Where(o => o.StructureId == StructureId)
    .Select(u => new UnitDto
       {
          u.StructureId,
          u.UnitId,
          // some other unit attributes
          UnitMedia = u.UnitMedia.Select(m => new UnitMediaDto
              {
                  m.Name,
                  // other unit media attributes
              }).ToList(),
          // etc.
       }).ToList();

Entity Framework is smart enough to generate a query that will include all the necessary data that it needs to compile the results into a result list, and then you can just serialize that list (or an individual result) to JSON. If you have a lot of data coming back from the different tables, this might perform poorly trying to do it all in one round-trip, so you may need to load the different DTOs one type at a time and construct your final UnitDtos a little more manually. But I'd start with this simple approach and only try something more complicated if you run into performance problems.

Definitely avoid doing separate round-trips per Unit result the way your original code does it. That will perform really badly if you have a lot of Units.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • Thank you for the answer; but i didn't really get how i should define my class then – Raiaki Mar 14 '22 at 20:42
  • That's something I can't help you with, because I don't know what your data looks like. You could honestly just remove the class names from the code above to create anonymous types instead. Or you could create classes and give them the properties that you expect to have appear on the JSON that you want to return. – StriplingWarrior Mar 14 '22 at 22:03