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

时间:2014-06-20 01:37:07

标签: c# linq entity-framework-5

我正在尝试从数据库查询中填充视图模型,该数据库查询包含来自父表和子表的数据。

我可以填充父视图模型,但它包含另一种类型的列表,我不知道如何使用linq填充此子列表。

以下是视图模型:

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; }
}

就我而言,这是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();

????是我不清楚的地方。我需要用fd范围变量填充它。虽然fd是另一种类型而不是galleryFileDescVM。

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:1)

您需要将FileDescendants作为子查询进行查询 - 您需要进行一些轻微的重新排列。

此结构应该这样做 - 我不确定您的架构,因此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();