1

I have completely working code, which returns the desired result.

var query = @$"SELECT 
    boats.*, 
    gallery.*,
    textblock.*,
    engine.*,
    generator.*,
    media.*
FROM {mainBoatTable} boats 
LEFT JOIN ViewVesselImageGallery gallery ON boats.BoatId = gallery.GalleryBoatId
LEFT JOIN ViewVesselTextBlock textblock ON boats.BoatId = textblock.TextBlockBoatId
LEFT JOIN ViewVesselEngine engine ON boats.BoatId = engine.EngineBoatId    
LEFT JOIN ViewVesselGenerator generator ON boats.BoatId = generator.GeneratorBoatId
LEFT JOIN ViewVesselVideoGallery media ON boats.BoatId = media.MediaBoatId

{boatFilter}
ORDER BY boats.BoatId, gallery.Sort, media.Sort";

var boatDict = new Dictionary<string, Listing>();

var boats = await _db.QueryAsync<Listing, Gallery, Textblock, Engine, Generator, Media, Listing >(
            query, (boat, gallery, textblock, engine, generator, media) =>
            {
                Listing currentBoat;
                if (!boatDict.TryGetValue(boat.BoatId, out currentBoat))
                {
                    currentBoat = boat;
                    
                    currentBoat.Gallery = new List<Gallery>();
                    currentBoat.Textblocks = new List<Textblock>();
                    currentBoat.Engines = new List<Engine>();
                    currentBoat.Generators = new List<Generator>();
                    currentBoat.Media = new List<Media>();

                    boatDict.Add(boat.BoatId, currentBoat);
                }

// Add Gallery if not already included
if (gallery != null && !currentBoat.Gallery.Any(g => g.GalleryID == gallery.GalleryID))
{
    currentBoat.Gallery.Add(gallery);
}

// Add Textblock if not already included
if (textblock != null && !currentBoat.Textblocks.Any(t => t.TextBlockId == textblock.TextBlockId))
{
    currentBoat.Textblocks.Add(textblock);
}

// Add Engine if not already included
if (engine != null && !currentBoat.Engines.Any(e => e.EngineId == engine.EngineId))
{
    currentBoat.Engines.Add(engine);
}

// Add Generator if not already included
if (generator != null && !currentBoat.Generators.Any(e => e.GeneratorId == generator.GeneratorId))
{
    currentBoat.Generators.Add(generator);
}

// Add Media if not already included
if (media != null && !currentBoat.Media.Any(e => e.MediaId == media.MediaId))
{
    currentBoat.Media.Add(media);
}

return currentBoat;


splitOn: "GalleryID,TextBlockId,EngineId,GeneratorId,MediaId"
        );

return boatDict.Values.ToList();

Media node is coming as expected

public class Media 
{
    public string MediaId { get; set; }
    public string MediaBoatId { get; set; }

    public string YoutubeVideoID { get; set; } // YouTube code for video
    public string YoutubeVideoTitle { get; set; }
    public string ShareType { get; set; }
    public int Sort { get; set; }

    public string DateTimeStamp { get; set; }
}

Now the challenge is they have changed the definition of Media object to take care of other video sources. So new objects are as follows: YoutubeID object is now nested inside Media with a list and same is the case for Vimeo.

public class Media 
{
    public List<YoutubeID> YoutubeIDs { get; set; }
    public List<VimeoID> VimeoIDs { get; set; }
}

public class YoutubeID
{
    public string MediaId { get; set; }
    public string MediaBoatId { get; set; }

    public string YoutubeVideoID { get; set; }  // YouTube code for video
    public string YoutubeVideoTitle { get; set; }
    public string ShareType { get; set; }
    public int Sort { get; set; }

    public string DateTimeStamp { get; set; }
}

Now the adding media code is failing, as Media ID is no more available in the root of Media, please advise what would be the solution for this.

if (media != null && !currentBoat.Media.Any(e => e.MediaId == media.MediaId))

Now e.YoutubeIDs is accessible but not any further. and result should come in further nested something like this:

"Media": {"YoutubeIDs": [{
    "MediaId": "859414",
    "MediaBoatId": "597017",
    "YoutubeVideoID": "7P1XORrlPNo",
    "YoutubeVideoTitle": "2016 Lagoon 450 - CONSPIRASEA",
    "ShareType": "Yes",
    "Sort": 1,
    "DateTimeStamp": "03/03/2025 16:46:22"
}],

Do we need to change the approach completely, I tried different ways to access the inner object, but couldn't get access. I have been exploring other StackOverFlow posting, not able to find the same problem. Please advise & direct me to right path.

3
  • 1
    I want to confirm with you: after you modified the definition of the Media class, did you apply the migration to update the database? Have the tables in the database changed? Or did you only modify the class definition without affecting the database? If the database does not change, it is better to create a MediaDTO class which contains the YoutubeIDs and VimeoIDs, then when query the Media table, you can convert the result to MediaDTO, and when insert new item into database, you can convert the MediaDTO to Media. Commented Mar 17 at 3:25
  • Just to clarify, no changes in the database front. I just moved YouTubeId object inside the Media Object. Media object is nothing but MediaDTO as you have suggested. The Challenge is in the QueryAsync, it is not considering YouTubeId as nested object.. Commented Mar 17 at 11:11
  • public class Media { public List<YoutubeID> YoutubeIDs { get; set; } public List<VimeoID> VimeoIDs { get; set; } } Posted in the question Commented Mar 17 at 11:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.