0

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.

Example:

If this ICB has 10 resources associated but later 3 of resources were deleted, I want to list only the 7 remaining.

There is a way to implement a conditional for the .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);

CidaoPapito
  • 572
  • 1
  • 6
  • 21
  • possible duplicate of [How to filter nested collection Entity Framework objects?](http://stackoverflow.com/questions/7079378/how-to-filter-nested-collection-entity-framework-objects) – Edin May 27 '14 at 14:57
  • @Edin, this link does work for Code First. – CidaoPapito May 27 '14 at 15:05

1 Answers1

0

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.

tschmit007
  • 7,559
  • 2
  • 35
  • 43
  • It seems that will limite my query in return the ICB only if has no Resource deleted. I just want to return the resources that wasn't deleted, not compromise tehe return of my main object ICB. – CidaoPapito May 27 '14 at 15:07