繁体   English   中英

使用 ASP.NET MVC Core 中的工作单元模式,使用从另一个表中提取的 Id 列表从表中检索数据

[英]Retrieve data from a table using a list of Ids extracted from another table using the Unit of Work Pattern in ASP.NET MVC Core

目的是从技术员表中检索所有员工的列表。 而不是使用该 ID 列表,必须在评论表上执行搜索,从而生成这些员工的所有评论的列表。

我已经将工作单元和存储库模式用于数据访问。

   tech = unitOfWork.TechnicianRepository.GetAll().Where(b=>b.Branch =="Calgary").AsEnumerable().Select(r => new Review{TechnicianID =r.UserId, }).ToList();

比在将它发送到视图之前我将它投射到控制器中

      public IActionResult Index()
      {

        var result = ((IEnumerable)tech).Cast<Review>().ToList();
        return View(result);
      }

以下是返回的结果之一。

  [{"id":0,"technicianID":"c62a2746-1bfc-484f-a479-084e0be389a4","overAllTechRating":0,"origin":null,"name":null,"feedbackText":null,"date":"0001-01-01T00:00:00"}

因此,从这里可以看出,用户 ID 是从 Technicians 表中提取的,但是对于这些 ID,所有其他字段都显示为空。

以下是我发布问题以来到目前为止所尝试的更新:

尝试过的方法:-

    public IActionResult Index()
    {

         var result = unitOfWork.TechnicianRepository.GetAll().Where(b => b.Branch == "Calgary").AsEnumerable().Select(r=> new {r.UserId});

        List<Review> array = new List<Review>();
        while (result != null) {

            array.Add(unitOfWork.ReviewRepository.GetByGuidId(result.First().UserId));
        }
        return View(array);
    }

在尝试了这个之后,我能够将我的 UserID 传递给 UnitOfWork GetByGuidId() 函数,但是,它找不到对象,返回一个错误的请求。

这是它的样子:

    public virtual T GetByGuidId(Guid? Id)
    {
        return dbSet.Find(Id);
    }

主要原因可能是 Technician 对象未实例化,因此数据存储从未返回该对象。 它返回技术员 ID,因为它是同一个表中的外键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM