使用 Include() 时如何通过 id 获取单条记录

How to get the single record by id when using Include()

我正在尝试创建一个 linq 数据库调用来检索大量信息。用户可以拥有 ApplicationForms 和 ApplicationLogic

的列表

我从视图传递 MemberID 和 ApplicationID 的路由值。

我想从 linq 获取单个 ApplicationForm 和基于传递的 ApplicationID 的单个 ApplicationLogic。

   public async Task<Members> GetApplicationDetails(int MemberID, int ApplicationID)
    {
        var GetApplication = await _dbContext.Members
                                    .Include(x => x.Members_PersonalInformation)
                                    .Include(x => x.Members_BankRefundDetails)
                                    .Include(x => x.Members_ResidentialAddress)
                                    .Include(x => x.ApplicationForms)  //I want to get the application by ApplicationID
                                    .Include(x => x.ApplicationLogic)  //I want to get the logic by ApplicationID
                                    .Where(x => x.ID == MemberID)
                                    .SingleOrDefaultAsync();
        return GetApplication;
    }

我的会员模型

  public virtual ICollection<ApplicationForms> ApplicationForms { get; set; }
    public virtual ICollection<ApplicationLogic> ApplicationLogic { get; set; }
    public virtual ICollection<Members_PersonalInformation> Members_PersonalInformation { get; set; }
    public virtual ICollection<Members_BankRefundDetails> Members_BankRefundDetails { get; set; }
    public virtual ICollection<Members_ResidentialAddress> Members_ResidentialAddress { get; set; }

将您的成员属性类型更新为 T 而不是 ICollection。

public virtual ApplicationForms ApplicationForms { get; set; }
public virtual ApplicationLogic ApplicationLogic { get; set; }
public virtual ICollection< Members_PersonalInformation> Members_PersonalInformation { get; set; }
public virtual ICollection<Members_BankRefundDetails> Members_BankRefundDetails { get; set; }
public virtual ICollection<Members_ResidentialAddress> Members_ResidentialAddress { get; set; }

修改属性类型后@ShaneRay

 public async Task<Members> GetApplicationDetails(int MemberID, int ApplicationID)
    {
        var GetApplication = await _dbContext.Members
                                   .Include(x => x.Members_PersonalInformation)
                                   .Include(x => x.Members_BankRefundDetails)
                                   .Include(x => x.Members_ResidentialAddress)
                                   .Include(x => x.ApplicationFormsSingle).Where(x => x.ApplicationFormsSingle.ID == ApplicationID)
                                   .Include(x => x.ApplicationLogicSingle).Where(x => x.ApplicationLogicSingle.ApplicationFormsID == ApplicationID)
                                   .SingleOrDefaultAsync(f => f.ID == MemberID);

        return GetApplication;
    }