[英]Entity Code First DbSet using include with conditions
I have a class that calls ICB that has a property of type list of ICBResource, that is possible to associate many Resources to one ICB, the problem is when I want to delete a Resource associated with ICB that is not truly deleted and only set true in an deleted boolean property, the resources deleted still return from DB. 我有一个调用ICB的类,该类具有ICBResource类型列表的属性,可以将许多资源关联到一个ICB,问题是当我要删除与ICB关联的资源时,该资源未真正删除,仅设置为true在已删除的布尔属性中,删除的资源仍从数据库返回。
Example: 例:
If this ICB has 10 resources associated but later 3 of resources were deleted, I want to list only the 7 remaining. 如果此ICB有10个关联资源,但后来删除了3个资源,我只想列出剩余的7个资源。
There is a way to implement a conditional for the .includes ? 有一种方法可以为.includes实现条件。
// ICB class
public class ICB
{
public ICB()
{
ICBResources = new List<ICBResource>();
}
public int ICBId { get; set; }
public string Location { get; set; }
public ICollection<ICBResource> ICBResources { get; set; }
}
// Resource class
public class ICBResource
{
public int ICBResourceId { get; set; }
public virtual ICB ICB { get; set; }
public string name{ get; set; }
public bool Deleted { get; set; }
}
//here is my Repository and how I select my ICB with the resources associated
public ICB GetICB(int id)
{
return GetDbSet<ICB>()
.Include("ICBResources")
.SingleOrDefault(i => i.ICBId == id);
}
There is a way to include another conditional like a Where(i => i.ICBResources.Where(r=> !r.deleted); 有一种方法可以包含另一个条件,例如Where(i => i.ICBResources.Where(r =>!r.deleted);
for example 例如
public ICB GetICB(int id)
{
return GetDbSet<ICB>()
.Include("ICBResources")
.SingleOrDefault(i => i.ICBId == id && i.ICBResources.Where(r => !r.Deleted).Count() == 0);
}
If the question is about filtering related, the answer is no. 如果问题与过滤有关,那么答案是否定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.