How do I join two entities in Entity Framework using lambda expression?

254 Views Asked by At

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; }
    }
0

There are 0 best solutions below