LINQ Lambda 用于创建包含来自 3 个相关表的信息的列表

LINQ Lambda for create a list with info from 3 related tables

我正在尝试使用 Receipt table 中的信息创建一个 ViewModel 列表,然后如果 Reason table 中存在相关信息,则检索最后插入的描述(ReceiptId 相关)并添加它(如果不存在只需将 null) 传递给 ViewModel 收据列表 (RejectDescription)。这是数据库模型:

Database Model

我尝试了很多方法来实现这一点,目前这是对我部分有效的代码,我说部分是因为在 RejectDescription 中保存 Reason.Description 如果它存在,否则只需传递 null 就可以了。

主要问题是当有很多 Reason.Description 时,它不会 return 并保存最后插入的一个(最近的,就是我要找的那个)。这是我的代码:

    [HttpPost]
    public ActionResult ReceiptList(string Keyword)
    {
        using (SEPRETEntities DBC = new SEPRETEntities())
        {
            long UserId = (long)Session["Id"];
            IEnumerable<Receipt> receipts = DBC.Receipts.Where(x => x.PersonId == UserId && x.Active == true).ToList();

            #region Search
            if (!string.IsNullOrEmpty(Keyword))
            {
                Keyword = Keyword.ToLower();

                receipts = receipts.Where(x => x.Person.Name.ToLower().Contains(Keyword) ||
                x.Person.MiddleName.ToLower().Contains(Keyword) ||
                x.Person.LastName.ToLower().Contains(Keyword) ||
                x.Person.Email.ToLower().Contains(Keyword) ||
                x.Person.Enrollment.ToLower().Contains(Keyword) ||
                x.Person.Career.ToLower().Contains(Keyword) ||
                x.Payment.Name.ToLower().Contains(Keyword) ||
                x.Payment.Price.ToString().ToLower().Contains(Keyword) ||
                x.Method.Name.ToLower().Contains(Keyword) ||
                x.Phase.Name.ToLower().Contains(Keyword) ||
                x.TimeCreated.ToString().ToLower().Contains(Keyword) ||
                x.Voucher.ToString().ToLower().Contains(Keyword)
                );
            }
            #endregion

            List<ReceiptVM> ReceiptList = receipts.Select(x => new ReceiptVM
            {
                Id = x.Id,
                PaymentId = x.PaymentId,
                Enrollment = x.Person.Enrollment,
                Career = x.Person.Career,
                PersonName = string.Concat(x.Person.Name, " ", x.Person.MiddleName, " ", x.Person.LastName),
                Email = x.Person.Email,
                PaymentName = x.Payment.Name,
                MethodName = x.Method.Name,
                Voucher = x.Voucher,
                Image = x.Image,
                PhaseId = x.Phase.Id,
                PriceFormatted = x.Payment.Price.ToString("C"),
                Active = x.Active,
                TimeCreatedFormatted = x.TimeCreated.ToString(),
                RejectDescription = x.Rejections.FirstOrDefault(y => y.ReasonId == y.Reason.Id)?.Reason.Description
            }).ToList();

            return PartialView("~/Views/Receipt/_SearchReceipt.cshtml", ReceiptList);
        }
    }

关于你的信息,我是 C# 的新手,ASP.NET MVC.Not 确定是否有更好的方法来实现这个或其他东西,非常感谢任何建议或技巧。

谢谢你,抱歉我的英语不好

您必须按 Id 排序拒绝原因,这将获取最近的原因,如下所示:

RejectDescription = x.Rejections.OrderByDescending(x=>x.Reason.Id).FirstOrDefault(y => y.ReasonId == y.Reason.Id)?.Reason.Description

或者您可以使用 LastOrDefault 获取最近的一个,如下所示: RejectDescription = x.Rejections.LastOrDefault(y => y.ReasonId == y.Reason.Id)?.Reason.Description

 List<ReceiptVM> ReceiptList = receipts.Select(x => new ReceiptVM
                {
                    Id = x.Id,
                    PaymentId = x.PaymentId,
                    Enrollment = x.Person.Enrollment,
                    Career = x.Person.Career,
                    PersonName = string.Concat(x.Person.Name, " ", x.Person.MiddleName, " ", x.Person.LastName),
                    Email = x.Person.Email,
                    PaymentName = x.Payment.Name,
                    MethodName = x.Method.Name,
                    Voucher = x.Voucher,
                    Image = x.Image,
                    PhaseId = x.Phase.Id,
                    PriceFormatted = x.Payment.Price.ToString("C"),
                    Active = x.Active,
                    TimeCreatedFormatted = x.TimeCreated.ToString(),
                    RejectDescription = x.Rejections.OrderByDescending(x=>x.Reason.Id).FirstOrDefault(y => y.ReasonId == y.Reason.Id)?.Reason.Description
                }).ToList();