使用Entity Framework填充IList集合

时间:2014-06-05 07:17:58

标签: c# linq

以下是实体类

public partial class TestPlanViewModel
{
    public TestPlanViewModel()
    {
        this.TestPlanTestPoints = new List<TestPlanTestPoint>();
    }

    public int TestPlanId { get; set; }

    public string TestPlanName { get; set; }

    public virtual IList<TestPlanTestPoint> TestPlanTestPoints { get; set; }
}

public class TestPlanTestPoint
{
    public int OrganizationId { get; set; }

    public int TestPlanId { get; set; }

    public string TestPlanName { get; set; }

    public int TestPlanVersion { get; set; }

    public int TestPointId { get; set; }

    public string TestPointName { get; set; }
}

我需要编写一个查询,我需要从dbContext中获取集合,

var query = (from tpm in TestPlanMaster
                         join tptp in TestPlanTestPoint on tpm.TestPlanId equals tptp.TestPlanId 
                         join tmm in TestMethodMaster on tptp.TestMethodId equals tmm.TestMethodId
                         join tpma in TestPointMaster on tptp.TestPointId equals tpma.TestPointId
                         select new
                         {
                             //Plan Details
                             tpm.TestPlanId,
                             tpm.TestPlanName,


                         }).ToList().Select(x => new Entities.CustomEntities.TestPlanViewModel ====> Custom Entity
                        {
                            TestPlanId = x.TestPlanId,
                            TestPlanName = x.TestPlanName,
                                                           TestPlanTestPoints = ?????? ==> how to fill this collection 
                        });

如上所示,TestPlanTestPointsIList集合对象。我需要使用TestPlanTestPoint table。

中的值填充数据

有什么建议吗?

模型 测试计划

public partial class TestPlanMaster
    {  
        [DataMember]
        public int TestPlanId { get; set; }

        [DataMember]
        public short TestPlanVersion { get; set; }

        [DataMember]
        public int OrganizationId { get; set; }

        [DataMember]
        public short TestPlanTypeId { get; set; }

        [DataMember]
        public string TestPlanName { get; set; }        
    }

TestPlanTestPointMapping模型

public partial class TestPlanTestPointMapping
    {

        [DataMember]
        public int OrganizationId { get; set; }

        [DataMember]
        public int TestPlanId { get; set; }

        [DataMember]
        public short TestPlanVersion { get; set; }

        [DataMember]
        public int TestPointId { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

你在想太多sql,想想更多linq。您想要选择“tpm”项,所以只需编写一个选项并选择新的TestPlanViewModel并填充其属性和导航属性。

我假设你的TestPlanMaster中有一个TestPlanTestPoints导航属性。

var q = (from tpm in TestPlanMaster
         select new TestPlanViewModel
         {
             TestPlanId = tpm.TestPlanId,
             TestPlanName = tpm.TestPlanName,
             TestPlanPoints = TestPlanTestPoint.Where(pr => tpm.TestPlanId == pr.TestPlanId).Select(tptp => new TestPlanTestPoint()
             {
                 // Fill properties here
             }             
         }).ToList();
相关问题