【问题标题】:How to write LINQ Query in instance table for many to many relationship如何在实例表中为多对多关系编写 LINQ 查询
【发布时间】:2018-02-13 01:29:00
【问题描述】:

我是 LINQ 和 Enitty 框架的初学者,我被困在一个地方。我在“Referral”和“Referrer”表之间有多对多的关系。我已将其命名为“ReferralInstance”表。

推荐模型

 public class Referral
    {
        public Referral()
        {
            ReferralInstances = new Collection<ReferralInstance>();
        }

        [Key]
        public int ReferralId { get; set; }
        public ICollection<ReferralInstance> ReferralInstances { get; set; }
    }

推荐人型号:

Public class ApplicationUser
  {
       public ApplicationUser()
       {
            ReferralInstances = new Collection<ReferralInstance>();
       }
       [Key]
       public string ReferrerId {get;set;}
       public ICollection<ReferralInstance> ReferralInstances { get; set; }
       public string FirstName { get; set; }
       public string LastName { get; set; }
  }

实例表

public class ReferralInstance
{
    [Key]
    public int ReferralInstanceId { get; set; }
    public bool ReferralStatus { get; set; }

    public int ReferralId { get; set; }
    public virtual Referral Referral { get; set; }

    public string ReferrerId { get; set; }
    public virtual ApplicationUser Referrer { get; set; }
}

流畅的 API:

 dBModelBuilder.Entity<Referral>()
                  .HasMany(r => r.ReferralInstances)
                  .WithRequired(r => r.Referral)
                  .HasForeignKey(r => r.ReferralId);

  dBModelBuilder.Entity<ApplicationUser>()
                 .HasMany(r => r.ReferralInstances)
                 .WithRequired(r => r.Referrer)
                 .HasForeignKey(r => r.ReferrerId).
                 WillCascadeOnDelete(false);

现在,我将从 UI 中获得一个输入 ReferrerId,例如 ABC。我想获取 FK 为 ABC 的 ReferralInstances 列表,然后我想获取所有相应的推荐列表。

我的新手尝试如下:

public ActionResult ReferredCandidates()
        {
            var referrerId = User.Identity.GetUserId();
            // trying to get list of "ReferralInstance"
            var referralInstanceList = _context.ReferralInstances.Where(r => (r.ReferrerId == referrerId)).Select(r => r.ReferralId);
            // Now I want to get all the referrals that match that id list I got above
            // what to do here
            // var referralss = _context.Referrals.Where(r => (r.ReferrerId  r.ReferralInstances.);

            return View(referrals);
        } 

示例:

推荐表


ReferralId       Text
1                HelloWorld
2                FooBar
3                HelloFoo

ReferralInstance 表:


 PK     ReferralId       ReferrerId

  1         1              ABC   
  2         1              DEF
  3         1              XYZ
  4         2              ABC
  5         3              DEF

推荐人表


    ReferrerId           Flag
    ABC                   USA   
    XYZ                   INDIA
    DEF                   UK

现在假设我的条目是 ABC,那么我想要下面的结果

Result Set

ReferralId         Text
1                 HelloWorld
2                  FooBar

【问题讨论】:

    标签: c# asp.net-mvc entity-framework linq asp.net-mvc-5


    【解决方案1】:

    可以直接查询导航属性ReferralInstance.Referral:

    var referrals = _context.ReferralInstances
        .Where(r => r.ReferrerId == referrerId)
        .Select(r => r.Referral);
    

    也许您想将Distinct() 添加到查询中。

    【讨论】:

    • 这看起来也很有效。 :) 谢谢先生.. :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    相关资源
    最近更新 更多