0

0

我是实体框架的新手。

我正在使用抽象存储库模式。

请参阅我有 2 个表(实体)。

检查报告和项目。

Projects 的 ID 字段在 InspectionReport 中是外来的。

我已经成功地分别从 InspectionReport 加载了数据。但我想根据 InspectionReport 中的 ProjectID 从项目表中加载项目名称。

我已经尝试过了,但它不起作用。

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

完整代码:

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

检验报告类:

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


        public int ProjectID { get; set; }

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

        }}

项目.cs

public class Projects
    {       
        public int ID { get; set; }
        public string ProjectNo { get; set; }
    }
4

0 回答 0