Linq查询中有一个非db对象

时间:2014-03-27 19:42:36

标签: linq linq-to-entities asp.net-mvc-5

我有多个View Model类,它们使用Linq从数据库中获取数据(DBContext类具有类似的结构但有更多字段)

我缺乏从一个类对象获取数据的知识,因为它包含在没有相应db对象的包装器类中。

提前感谢您对此提供任何帮助或建议。

以下是类结构:

public class TopLevelViewModel
{
    public TopLevelID  { get; set; }
    public TopLevelTitle  { get; set; }
    public List<SecondLevelViewModel> SeconLevel { set; get; }

    public TopLevelViewModel()
    {
           Inner = new List<SecondLevelViewModel>();
    }
}

public class SecondLevelViewModel
{
    public SecondLevelID  { get; set; }
    public SecondLevelTitle  { get; set; }
    public TopLevelID  { get; set; }
    public WrapperViewModel  { get; set; }
}   

public class InnerViewModel
{
    public InnerID  { get; set; }
    public InnerTitle  { get; set; }
    public SecondLevelID  { get; set; }
}

public class WrapperViewModel
{
    public List<InnerViewModel> Inner { set; get; }

    public WrapperViewModel()
    {
        Inner = new List<InnerViewModel>();
    }
}

Linq查询获取上述数据库对象的数据:

List<TopLevelViewModel> data = null;
data = db.TopLevel.Where(t => t.TopLevelID == URLTopLevelID)
                  .Select(t => new TopLevelViewModel()
                                   {
                                      TopLevelID = t.TopLevelID,
                                      TopLevelTitle = t.TopLevelTitle,
                                      SeconLevel = db.SecondLevel.Where(s => s.TopLevelID == t.TopLevelID)
          .Select(s => new SecondLevelViewModel()
                           {
                                SecondLevelID = s.SecondLevelID, 
                                SeconLevelTitle = s.SeconLevelTitle,              
                                WrapperViewModel = ???
                           }).ToList<SecondLevelViewModel>()
                }).ToList < TopLevelViewModel();

查询以获取内部对象数据:

db.Inner.Where(i => i.SecondLevelID == s.SecondLevelID)
        .Select(i => new InnerViewModel()
                         {
                             InnerID  = i.InnerID,
                             InnerTitle = i.InnerTitle
                         }).ToList<InnerViewModel>() 

问题:如何将上述内部查询合并为WrapperViewModel类的一部分,并将主Linq查询中的内容作为单个查询?

WrapperViewModel没有DB对象,但它是InnerViewModel类的包装类。

1 个答案:

答案 0 :(得分:0)

我想我明白了。希望它对某人有用。如果有任何想法/建议请分享!

单个查询将是:

       List<TopLevelViewModel> data = null ;
        data = db.TopLevel.Where(t => t.TopLevelID == URLTopLevelID).Select(t => new TopLevelViewModel()
                {
                    TopLevelID = t.TopLevelID,
        TopLevelTitle = t.TopLevelTitle,
                    SeconLevel = db.SecondLevel.Where(s => s.TopLevelID == t.TopLevelID).Select(s => new SecondLevelViewModel()
                    {
                        SecondLevelID = s.SecondLevelID, 
            SeconLevelTitle = s.SeconLevelTitle,              
                        WrapperViewModel = new WrapperViewModel { Inner = 
                                        db.Inner.Where(i => i.SecondLevelID == s.SecondLevelID).Select(i => new InnerViewModel()
                                            {
                                               InnerID  = i.InnerID,
                       InnerTitle = i.InnerTitle
                                            }).ToList<InnerViewModel>() 
                    }).ToList<SecondLevelViewModel>()
                }).ToList < TopLevelViewModel();