K
K
kolenchits862016-10-11 23:50:52
.NET
kolenchits86, 2016-10-11 23:50:52

How to filter result in multiple include entity framework 6?

Hello.
There is a set of relations of the following types.
There are many categories of attributes.
Each category has many attributes.
Each project has its own values ​​for each attribute.
The result is a set of classes with such keys

public class Project : ModelBase<Project>
    {
// Категории аттрибутов

        private List<ProjectAttributeCategoryDictionary> projectAttributeCategoryDictionaries;
        public List<ProjectAttributeCategoryDictionary> ProjectAttributeCategoryDictionaries
        {
            get { return projectAttributeCategoryDictionaries; }
            set
            {
                projectAttributeCategoryDictionaries = value;
                NotifyPropertyChanged(m => m.ProjectAttributeCategoryDictionaries);
            }
        }

        // Аттрибуты со значениями

        private List<Attributes> attributes;
        public List<Attributes> Attributes
        {
            get { return attributes; }
            set
            {
                attributes = value;
                NotifyPropertyChanged(m => m.Attributes);
            }
        }
}

public class ProjectAttributeCategoryDictionary : ModelBase<ProjectAttributeCategoryDictionary>
    {
private List<ProjectAttributeDictionary> attributeDictionaries;
        public List<ProjectAttributeDictionary> AttributeDictionaries
        {
            get { return attributeDictionaries; }
            set
            {
                attributeDictionaries = value;
                NotifyPropertyChanged(m => m.AttributeDictionaries);
            }
        }
}

public class ProjectAttributeDictionary : ModelBase<ProjectAttributeDictionary>
    {
private List<Attributes> attributes;
        public List<Attributes> Attributes
        {
            get { return attributes; }
            set
            {
                attributes = value;
                NotifyPropertyChanged(m => m.Attributes);
            }
        }
}

public class Attributes : ModelBase<Attributes>
    {
        public virtual Project Project { get; set; }
        public virtual ProjectAttributeDictionary ProjectAttributeDictionary { get; set; }
}

The next question is why the following selection may not work
foreach (var cat in db.ProjectAttributeCategoryDictionaries.
   Include(x => x.AttributeDictionaries.
      Select(t => t.Attributes.
      Where(y => y.Project.ProjectId == projectId))))
                {
                    
                }

The error The Include path expression must refer to a navigation property defined on the type appears. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
But all the same with foreach works fine, but not beautiful)
foreach (var cat in db.ProjectAttributeCategoryDictionaries.Include("AttributeDictionaries").Include("AttributeDictionaries.Attributes"))
                {
                    foreach (var ad in cat.AttributeDictionaries.ToArray())
                    {
                        foreach (var adAttribute in ad.Attributes.ToArray())
                        {
                            if (adAttribute.Project.ProjectId != projectId)
                            {
                                ad.Attributes.Remove(adAttribute);
                            }
                        }
                    }
                    categoryDictionary.Add(cat);
                }

it is interesting to understand why EF cuts my request

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question