填充模型类使用查询

时间:2015-06-01 19:35:23

标签: c# asp.net-mvc linq

您好我有以下型号

public class MainClass
{
    public List<SubClass1> First { get; set; }
    public List<SubClass2> Second { get; set; }
    public List<SubClass3> Third { get; set; }
}

public class SubClass1
{
    public string Prop1 { get; set; }
}

public class SubClass2
{
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }            
}

public class SubClass3
{
    public string Prop4 { get; set; }
    public string Prop5 { get; set; }
}

我需要使用linq查询结果“填充”MainClass,我试图这样做:

var resultado = from a in DataContext.Table1
                        join b in DataContext.Table2 on a.Id equals b.CId
                        select new MainClass
                        {
                           First = new SubClass1 ... list query result,

                           Second = new SubClass2 ... list query result,

                           Third = new SubClass3 ... list query result,                               
                        }; 

如何“填充”SubClasses以构造MainClass。

1 个答案:

答案 0 :(得分:1)

您的代码应如下所示

var resultado = from a in DataContext.Table1
                    join b in DataContext.Table2 on a.Id equals b.CId
                    select new MainClass
                    {
                       First = ( from i in db.Entity where somecondition).ToList<SubClass1>()
                       , Second = (from s in db.Entity2 where ).ToList<SubClass1>()

}

依旧......