最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dictionary - Nest Objects to be be nested one more level further in Dapper .NET API - Stack Overflow

programmeradmin1浏览0评论

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();

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.

发布评论

评论列表(0)

  1. 暂无评论