使用Include()时如何通过ID获取单个记录

时间:2019-03-22 01:27:43

标签: c# linq ef-core-2.2

我正在尝试创建linq数据库调用以检索一堆信息。用户可以具有ApplicationForms和ApplicationLogic的列表

从View即时消息传递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; }

2 个答案:

答案 0 :(得分:0)

将成员属性类型更新为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; }

答案 1 :(得分:-1)

修改了@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;
    }