Linq查询,其中有一个非数据库对象



我有多个视图模型类,使用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查询获取上述db对象的数据:

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类的包装类。

我想我明白了。希望对别人有用。如果有任何想法或建议,请分享!

单个查询将是:

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

相关内容

  • 没有找到相关文章

最新更新