简体   繁体   English

LINQ中的嵌套列表数据库查询

[英]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. 我可以在很大程度上填充父视图模型,但是它包含其他类型的列表,并且我不确定如何使用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: 就我所知这是LINQ:

                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. 我需要用fd range变量填充它。 Though fd is of another type than galleryFileDescVM. 尽管fd是不同于galleryFileDescVM的另一种类型。

Thanks so much for your help! 非常感谢你的帮助!

You need to query the FileDescendants as a sub-query - some slight rearrangement is all you really need. 您需要以子查询的形式查询FileDescendants您真正需要的是一些轻微的重新排列。

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 此结构应该可以实现-我不确定您的架构,因此将关闭galleryFileDescVM分配,但应该为您指明正确的方向

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM