I have MainClass
and a ChildData
.
I need to get ChildData
given a MainClass
.
The MainClass
has a Property called ChildClassProperty
. ChildData
has got a ForeignKey to MainClass
. How do I get the ChildData
using LINQ?
so far:
using (var ctx = Csla.Data.ObjectContextManager<DB.Data.Entities>.GetManager(Model.EntitiesDatabase.Name))
{
var xxx = from a in ctx.ObjectContext.ChildData where a...
}
ChildData
has a foreign key to MainClass
. Do something like:
Entities:
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; }
}
Queries:
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);
}
or by passing in a full MainClass
instance:
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);
}