通过ForeignKey获取数据

时间:2014-02-05 10:20:38

标签: c# sql linq entity-framework csla

我有MainClassChildData

我需要ChildDataMainClass

MainClass有一个名为ChildClassProperty的属性。 ChildData已获得MainClass的ForeignKey。如何使用LINQ获取ChildData

到目前为止:

using (var ctx = Csla.Data.ObjectContextManager<DB.Data.Entities>.GetManager(Model.EntitiesDatabase.Name))
{
    var xxx = from a in ctx.ObjectContext.ChildData where a...
}

1 个答案:

答案 0 :(得分:0)

ChildDataMainClass的外键。做类似的事情:

<强>实体:

public ChildData
{
    public int Id { get; set; } // primary key
    public int MainClassId { get; set; } // foreign key
    public string Data { get; set; }
}

public MainClass
{
    public int Id { get; set; } // primary key
    public string Data { get; set; }
}

<强>查询:

public IQueryable<ChildData> GetChildData(int mainClassId)

using (var ctx = Csla.Data.ObjectContextManager<DB.Data.Entities>.GetManager(Model.EntitiesDatabase.Name))
{
    return ctx.ObjectContext.ChildData.Where(x => x.MainClassId == mainClassId);
}

或通过传递完整的MainClass实例:

public IQueryable<ChildData> GetChildData(MainClass mainClass)

using (var ctx = Csla.Data.ObjectContextManager<DB.Data.Entities>.GetManager(Model.EntitiesDatabase.Name))
{
    return ctx.ObjectContext.ChildData.Where(x => x.MainClassId == mainClass.Id);
}