Julian Dormon
Julian Dormon

Reputation: 1779

Nested list database query in linq

I am trying to fill a view model from a database query which includes data from a parent and child tables.

I can fill the parent view model for the most part, but it includes a list of another type and I am not sure how to fill this child list using linq.

Here are the view models:

public class galleryFileVM
{
    public galleryFileVM()
    {
        this.galleryFileDescVMs = new List<galleryFileDescVM>();
    }

    public int MediaId { get; set; }
    public int FileTypeId { get; set; }
    public string FileName { get; set; }
    public string FileNameSlug { get; set; }
    public int SortOrder { get; set; }
    public int OrigH { get; set; }
    public int OrigW { get; set; }
    public List<galleryFileDescVM> galleryFileDescVMs { get; set; }

}

public class galleryFileDescVM
{
    public string Drive { get; set; }
    public string Prefix { get; set; }
    public string RdmPrefix { get; set; }
    public int FileTypeId { get; set; }
    public int FileWidth { get; set; }
    public int FileHeight { get; set; }
    public string FillClass { get; set; }
}

And here is the LINQ as far as I got:

                PagePublishVM.galleryFileVMs = (
                from tn in db.Media
                from fd in db.FileDescendants
                where tn.GalleryId == galleryId && fd.FileId == tn.FileId
                orderby tn.SortOrder

                select new galleryFileVM
                {
                    MediaId = tn.MediaId,
                    FileTypeId = (int)tn.File.FileTypeId,
                    FileName = tn.File.FileName,
                    FileNameSlug = tn.File.FileNameSlug,
                    SortOrder = tn.SortOrder,
                    OrigH = tn.OrigHeight,
                    OrigW = tn.OrigWidth,
                    galleryFileDescVMs = ????(this should be a list<galleryFileDescVM>)

                })
                .ToList();

The ???? is where I am unclear. I need to fill this with the fd range variable. Though fd is of another type than galleryFileDescVM.

Thanks so much for your help!

Upvotes: 0

Views: 645

Answers (1)

eouw0o83hf
eouw0o83hf

Reputation: 9598

You need to query the FileDescendants as a sub-query - some slight rearrangement is all you really need.

This structure should do it - I'm unsure of your schema so the galleryFileDescVM assignment will be off, but it should point you in the right direction

PagePublishVM.galleryFileVMs = (
            from tn in db.Media
            where tn.GalleryId == galleryId
            orderby tn.SortOrder
            select new galleryFileVM
            {
                MediaId = tn.MediaId,
                FileTypeId = (int)tn.File.FileTypeId,
                FileName = tn.File.FileName,
                FileNameSlug = tn.File.FileNameSlug,
                SortOrder = tn.SortOrder,
                OrigH = tn.OrigHeight,
                OrigW = tn.OrigWidth,
                galleryFileDescVMs = (from fd in db.FileDescendants
                                        where fd.FileId == tn.FileId
                                        select new galleryFileDescVM
                                        {
                                            // Guessing here because I don't have the schema
                                            Drive = fd.Drive,
                                            Prefix = fd.Prefix,
                                            // etc.
                                        }).ToList()

            })
            .ToList();

Upvotes: 1

Related Questions