I have the following setup in Entity:
public class t_HT
{
public int UNID { get; set; }
public string Name { get; set; }
public ICollection<t_HTC> Comments { get; set; }
}
AND
public class t_HTC
{
public int UNID { get; set; }
public int HTID { get; set; }
[ForeignKey("HTID")]
public t_HT HT { get; set; }
public int CId { get; set; }
[ForeignKey("CId")]
public t_C C { get; set; }
}
AND
public class t_C
{
public int UNID { get; set; }
public string Name { get; set; }
public ICollection<t_HTC> Comments { get; set; }
}
Where the relationships are as follows:
Many t_HTC
to one t_HT
One t_HT
to many t_HTC
AND
Many t_HTC
to one t_C
One t_C
to many t_HTC
This setup works fine and achieves what I need.
However, when querying using C#
and Linq/Entity
I can do the following:
var queryHt = context.ht.include(x => x.htc);
AND
var queryC = context.c.include(x => x.htc);
Either of these will return a single t_ht
with a nested list of t_htc
OR
a single t_c
with a nested list of t_htc
However, what I want to achieve is:
a single t_ht
, with a nested list of t_htc
, and then for t_htc
to include the corresponding entry in t_c
I know I can achieve this by performing a join that joins queryC
into queryHt
but this seems a bit of a long way to go about doing this.
Surely entity can achieve what I am trying to do?
Please note variable names have been adjusted for the purpose of this question, and are not like this in my actual code.
You can achieve what you want with this:
var queryHt = context.ht.Include("htc.C");
or with the strong typed version:
var queryHt = context.ht.Include(x => x.htc.Select(y => y.C));
The strong typed version requires adding using System.Data.Entity;
.