实体框架..自我引用表..得到深度= x的记录? [英] Entity framework.. self referencing table.. get records of Depth =x?
本文介绍了实体框架..自我引用表..得到深度= x的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我成功地用在实体框架自我引用表。
但我无法弄清楚如何获得所需深度的记录?
应该是什么逻辑呢?
型号:
公共类FamilyLabel
{
公共FamilyLabel()
{
this.Children =新的收集和LT; FamilyLabel>();
this.Families =新的收集和LT;家庭>();
} [键]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)
公众诠释FamilyLabelId {搞定;组; }
公共字符串FamilyLabelName {搞定;组; } 公共虚拟FamilyLabel家长{搞定;组; } 公众诠释JamaatId {搞定;组; }
公共虚拟Jamaat的Jamaat的{搞定;组; } 公共虚拟的ICollection<家庭>家庭{搞定;组; }
公共虚拟的ICollection< FamilyLabel>儿童{搞定;组; }
}
解决方案
理论上你可以创建动态构建根据指定的深度级别的查询前pression的方法:
context.FamilyLabels.Where(X =>
x.Parent。 ... .Parent = NULL&放大器;!&安培;
x.Parent.Parent ... .Parent == NULL);
下面执行的伎俩:
公共静态的IList< FamilyLabel>获取(的DbConnection连接,诠释深度)
{
变种p值=前pression.Parameter(typeof运算(FamilyLabel));
防爆pression电流= P; 的for(int i = 0; I<深;我++)
{
电流=前pression.Property(目前,父);
} VAR nullConst =前pression.Constant(空的typeof(FamilyLabel)); VAR predicate =前pression.Lambda<&Func键LT; FamilyLabel,布尔>>(
防爆pression.AndAlso(
防爆pression.NotEqual(电流,nullConst)
防爆pression.Equal(前pression.Property(目前,父),nullConst)),P); 使用(MyDbContext上下文=新MyDbContext(连接))
{
返回context.FamilyLabels.Where(predicate).ToList();
}
}
不过,presumably这将创建一批加入前pressions的,所以也许这不是最优化的方式。
I am successfully using a self referencing table in entity framework. But I can't figure out how to get the records of the desired depth ?
What should be the logic for this ?
Model :
public class FamilyLabel
{
public FamilyLabel()
{
this.Children = new Collection<FamilyLabel>();
this.Families = new Collection<Family>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int FamilyLabelId { get; set; }
public string FamilyLabelName { get; set; }
public virtual FamilyLabel Parent { get; set; }
public int JamaatId { get; set; }
public virtual Jamaat Jamaat { get; set; }
public virtual ICollection<Family> Families { get; set; }
public virtual ICollection<FamilyLabel> Children { get; set; }
}
解决方案
Theoretically you can create a method that builds query expression dynamically based on the specified depth level:
context.FamilyLabels.Where(x =>
x.Parent. ... .Parent != null &&
x.Parent.Parent ... .Parent == null);
The following implementation does the trick:
public static IList<FamilyLabel> Get(DbConnection connection, int depth)
{
var p = Expression.Parameter(typeof(FamilyLabel));
Expression current = p;
for (int i = 0; i < deep; i++)
{
current = Expression.Property(current, "Parent");
}
var nullConst = Expression.Constant(null, typeof(FamilyLabel));
var predicate = Expression.Lambda<Func<FamilyLabel, bool>>(
Expression.AndAlso(
Expression.NotEqual(current, nullConst),
Expression.Equal(Expression.Property(current, "Parent"), nullConst)), p);
using (MyDbContext context = new MyDbContext(connection))
{
return context.FamilyLabels.Where(predicate).ToList();
}
}
However, presumably this will create a bunch of join expressions, so maybe this is not the most optimal way.
这篇关于实体框架..自我引用表..得到深度= x的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文