Entity Framework 嵌套变量
Entity Framework nesting variables
我在实体中有以下设置:
public class t_HT
{
public int UNID { get; set; }
public string Name { get; set; }
public ICollection<t_HTC> Comments { get; set; }
}
和
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; }
}
和
public class t_C
{
public int UNID { get; set; }
public string Name { get; set; }
public ICollection<t_HTC> Comments { get; set; }
}
其中关系如下:
多t_HTC
对一t_HT
一个 t_HT
到多个 t_HTC
和
多t_HTC
对一t_C
一个 t_C
到多个 t_HTC
此设置工作正常并实现了我的需要。
但是,当使用 C#
和 Linq/Entity
查询时,我可以执行以下操作:
var queryHt = context.ht.include(x => x.htc);
和
var queryC = context.c.include(x => x.htc);
其中任何一个都会 return 一个带有 t_htc
嵌套列表的单个 t_ht
或者
带有 t_htc
嵌套列表的单个 t_c
然而,我想达到的是:
单个 t_ht
,带有 t_htc
的嵌套列表,然后 t_htc
将相应的条目包含在 t_c
中
我知道我可以通过执行将 queryC
连接到 queryHt
的连接来实现此目的,但这似乎还有很长的路要走。
确定实体可以实现我想要做的事情吗?
请注意变量名已针对此问题进行了调整,在我的实际代码中并非如此。
你可以用这个实现你想要的:
var queryHt = context.ht.Include("htc.C");
var queryHt = context.ht.Include(x => x.htc.Select(y => y.C));
强类型版本需要添加using System.Data.Entity;
。
我在实体中有以下设置:
public class t_HT
{
public int UNID { get; set; }
public string Name { get; set; }
public ICollection<t_HTC> Comments { get; set; }
}
和
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; }
}
和
public class t_C
{
public int UNID { get; set; }
public string Name { get; set; }
public ICollection<t_HTC> Comments { get; set; }
}
其中关系如下:
多t_HTC
对一t_HT
一个 t_HT
到多个 t_HTC
和
多t_HTC
对一t_C
一个 t_C
到多个 t_HTC
此设置工作正常并实现了我的需要。
但是,当使用 C#
和 Linq/Entity
查询时,我可以执行以下操作:
var queryHt = context.ht.include(x => x.htc);
和
var queryC = context.c.include(x => x.htc);
其中任何一个都会 return 一个带有 t_htc
嵌套列表的单个 t_ht
或者
带有 t_htc
t_c
然而,我想达到的是:
单个 t_ht
,带有 t_htc
的嵌套列表,然后 t_htc
将相应的条目包含在 t_c
我知道我可以通过执行将 queryC
连接到 queryHt
的连接来实现此目的,但这似乎还有很长的路要走。
确定实体可以实现我想要做的事情吗?
请注意变量名已针对此问题进行了调整,在我的实际代码中并非如此。
你可以用这个实现你想要的:
var queryHt = context.ht.Include("htc.C");
var queryHt = context.ht.Include(x => x.htc.Select(y => y.C));
强类型版本需要添加using System.Data.Entity;
。