When using Entity Framework 6
, how is the most efficient way to create an object or objects with additional data from other DbSet
entities, when I have a DbContext
or IQueryable<T>
?
Here is some code:
If I have an Data
class as follows:
public class Data
{
[Key]
public int id { get; set; }
public string name { get; set; }
public string data { get; set; }
public int parentId { get; set; }
public int otherDataId { get; set; }
}
And an OtherData
class as follows:
public class OtherData
{
[Key]
public int id { get; set; }
public string name { get; set; }
public string data { get; set; }
}
In the Data
class, the parentId
is a foreign key reference to another Data
object in the same DbSet
, and the otherDataId
is a foreign key reference to an OtherData
object in a DbSet<OtherData>
.
I would like to get all Data
objects in the DbSet<Data>
, with the additional DbSet
data of the parent Data
objects id
and name
and the OtherData
object's id
and name
. I need this to be in one object to be sent from a webservice GET.
I am not sure on how to do this.
Do I need some code along the lines of:
var result = DbContext.Data.Select(x=> x...).Join(y=> y...) .. new { id = x.id... y.name.. }
Can I please have some help with this code?