LinQ: show list of parents and children who depend on a model value

2

I have two models, Category and ExternalResources:

Category

public partial class Category
{
    public long CategoryID { get; set; }
    public string Name { get; set; }
    public Nullable<long> ParentCategoryID { get; set; }

    public virtual ICollection<Category> Subcategories { get; set; }
    public virtual Category ParentCategory { get; set; }
    public virtual ICollection<ExternalResource> ExternalResources { get; set; }
}

ExternalResources

public partial class ExternalResource
{
    public string DisplayText { get; set; }
    public string URL { get; set; }
    public long CategoryID { get; set; }
    public int ExternalResourceID { get; set; }
    public bool SupportItem { get; set; }
    public bool IsInActivity { get; set; }

    public virtual Category Category { get; set; }
}

The variable called IsInActivity is the variable that I use to know whether to show it in the list or not.

I want to show a list of parents and their children that have the variable IsInActivity as true .

I made this function that returns almost what I need but it does not show the parents who have only children selected as true.

public List<Category> ContainsResources(List<Category> qry)
{
                List<Category> lstResourceIsInActivity = qry.Where(c => c.ExternalResources.Any(r => r.IsInActivity)).Select(c => new Category
                {
                    CategoryID = c.CategoryID,
                    Name = c.Name,
                    ParentCategoryID = c.ParentCategoryID,
                    Subcategories = c.Subcategories.Where(b => b.ExternalResources.Any(r => r.IsInActivity) && b.ParentCategoryID != null).Select(b => new Category
                    {
                        CategoryID = b.CategoryID,
                        Name = b.Name,
                        ParentCategoryID = b.ParentCategoryID,
                        ExternalResources = b.ExternalResources.Where(r => r.IsInActivity).ToList()
                    }).ToList(),
                    ParentCategory = c.ParentCategory,
                    ExternalResources = c.ExternalResources.Where(r => r.IsInActivity).ToList()
                })
             .ToList();

                return lstResourceIsInActivity;
    }

Is there any way to do this with LinQ?

UPDATE

    
asked by Jason0495 21.05.2018 в 16:04
source

1 answer

0

The easiest way to do it would be to create a function in the Category class. Being a partial class (possibly generated by EntityFramework) we can create another file with the same namespace of the Category class.

public partial class Category
{
    public bool FiltrarIsInActivity()
    {
        if (ExternalResources != null)
        {
            // Filtramos external resources por IsInActivity
            ExternalResources = ExternalResources.Where(e => e.IsInActivity).ToList();

            // Si no existe algun external resources la categoria no esta activa
            if(!ExternalResources.Any()){
               return false;
            }

            // Filtramos las subcategorias por la funcion actual
            Subcategories = Subcategories.Where(s => s.FiltrarIsInActivity()).ToList();


            return true;
        }

        return false;
    }
}

The code of the ContainsResources function would be the following:

public List<Category> ContainsResources(List<Category> qry)
{
        var filtrados = qry.Where(c => c.FiltrarIsInActivity());

        return filtrados.ToList();
}
    
answered by 10.06.2018 в 13:17