0

0

I am new to Entity Framework.

I am using abstract repository pattern.

See I have 2 tables (Entities).

InspectionReport and Projects.

ID field of Projects is foreign in InspectionReport.

I have successfully loaded the data from InspectionReport, individually. But I want to load the name of Project from projects table against the ProjectID in InspectionReport.

I have tried this but it doesn't work.

 public List<InspectionReport> GetInspectionReportListFiltered()
        {
            List<InspectionReport> InspectionReportList = new List<InspectionReport>();
            var query = uow.InspectionReportRepository.GetQueryable().AsQueryable();

            query = ququery.Join(uow.ProjectsRepository.GetQueryable().AsQueryable(), InspectionRep => InspectionRep.ProjectID, project => project.ID, (InspectionRep, project) => new {InspectionReport= InspectionRep, Projects= project });

            InspectionReportList = query.ToList();

            return InspectionReportList;
        }

Full code:

      private AbstractRepository<InspectionReport> InspectionReportRepo;
        private AbstractRepository<Projects> ProjectsRepo;

 public AbstractRepository<InspectionReport> InspectionReportRepository
        {
            get
            {
                if (this.InspectionReportRepo == null)
                {
                    this.InspectionReportRepo = new AbstractRepository<InspectionReport>(context);
                }
                return InspectionReportRepo;
            }
        }

public AbstractRepository<Projects> ProjectsRepository
        {
            get
            {
                if (this.ProjectsRepo == null)
                {
                    this.ProjectsRepo = new AbstractRepository<Projects>(context);
                }
                return ProjectsRepo;
            }
        }

InspectionReport class:

public class InspectionReport
    {
        //General Details
        public int InspectionReportID { get; set; }


        public int ProjectID { get; set; }

        [ForeignKey("ProjectID")]
        public virtual Projects Projects { get; set; }

        }}

Projects.cs

public class Projects
    {       
        public int ID { get; set; }
        public string ProjectNo { get; set; }
    }
CodingManiac
  • 123
  • 1
  • 1
  • 9
  • 2
    You can use the Include() function in EF6 to include the Project data with your report: https://learn.microsoft.com/en-us/ef/ef6/querying/related-data – Jon Apr 21 '19 at 12:57
  • can you make my code that way? I tried my best – CodingManiac Apr 21 '19 at 13:26
  • Post what you have tried? Did you look at the examples, they are fairly straightforward. You just need to include the Projects entity in your DBContext query – Jon Apr 21 '19 at 13:34
  • The non-Repository LINQ code would be something like `var inspectionReportsWithProject = context.InspectionReport.Include(ir => ir.Projects).ToList();` I would also recommend changing `Projects` to `Project` so as not to imply it is a collection. – Steve Greene Apr 23 '19 at 13:51

0 Answers0