2016-06-24 50 views
0

我正在做我的第一個項目,有許多關係,我仍然是新手,因此請原諒基本問題。我工作的項目有許多實體(人員,業務等),每個實體可以有許多電話號碼(家庭,工作,單元等)。我必須做一個電話號碼搜索,將返回所有與他們有關的電話號碼的實體(在這個例子中只有人)。當我擁有NumberID時,我無法弄清楚如何訪問entityID!實體框架多對多關係訪問數據

型號:

namespace CaseManagement.Models 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class IdentityNumber 
    { 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
     public IdentityNumber() 
     { 
      this.IdentityEntities = new HashSet<IdentityEntity>(); 
     } 

     public int NumberID { get; set; } 
     public System.DateTime DateCreated { get; set; } 
     public string TenDigitNumber { get; set; } 
     public bool Preferred { get; set; } 
     public byte[] RowVersion { get; set; } 
     public string ChangedBy { get; set; } 
     public int NumberTypeID { get; set; } 

     public virtual IdentityNumberType IdentityNumberType { get; set; } 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<IdentityEntity> IdentityEntities { get; set; } 
    } 
} 


    namespace CaseManagement.Models 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class IdentityEntity 
    { 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
     public IdentityEntity() 
     { 
      this.ContactLogs = new HashSet<ContactLog>(); 
      this.IdentityEntityCasePartyJoins = new HashSet<IdentityEntityCasePartyJoin>(); 
      this.Files = new HashSet<File>(); 
      this.IdentityAddresses = new HashSet<IdentityAddress>(); 
      this.IdentityBusinesses = new HashSet<IdentityBusiness>(); 
      this.IdentityEmails = new HashSet<IdentityEmail>(); 
      this.IdentityNumbers = new HashSet<IdentityNumber>(); 
      this.IdentityPersonNames = new HashSet<IdentityPersonName>(); 
      this.IdentityPersonPositions = new HashSet<IdentityPersonPosition>(); 
      this.Notes = new HashSet<Note>(); 
     } 

     public int EntityID { get; set; } 
     public int EntityTypeID { get; set; } 
     public System.DateTime DateCreated { get; set; } 
     public byte[] RowVersion { get; set; } 
     public string ChangedBy { get; set; } 

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<ContactLog> ContactLogs { get; set; } 
     public virtual IdentityEntityType IdentityEntityType { get; set; } 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<IdentityEntityCasePartyJoin> IdentityEntityCasePartyJoins { get; set; } 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<File> Files { get; set; } 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<IdentityAddress> IdentityAddresses { get; set; } 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<IdentityBusiness> IdentityBusinesses { get; set; } 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<IdentityEmail> IdentityEmails { get; set; } 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<IdentityNumber> IdentityNumbers { get; set; } 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<IdentityPersonName> IdentityPersonNames { get; set; } 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<IdentityPersonPosition> IdentityPersonPositions { get; set; } 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<Note> Notes { get; set; } 
    } 
} 

Join Table

控制器:

namespace CaseManagement.Controllers 
{ 
    public class IdentityPersonSelectController : Controller 
    { 
     private CaseMGMTEntities db = new CaseMGMTEntities(); 
     // GET: IdentityPersonSelect 
     public ActionResult Index(int id) 
     { 
      var phoneLogs = db.PhoneLogs.Include(p => p.ContactInquirySource).Include(p => p.ContactReason).Include(p => p.EthicsEmployee); 
      var recordFromPhoneLog = (from x in phoneLogs 
          where x.PhoneLogID == id 
          select x).First(); 
      var person = db.IdentityEntities.Include(p => p.IdentityAddresses).Include(p => p.IdentityEmails).Include(p => p.IdentityNumbers).Include(p => p.IdentityPersonNames).Include(p => p.IdentityPersonPositions); 



      var sameName = (from x in db.IdentityPersonNames 
          where x.FirstName == recordFromPhoneLog.FirstName.ToLower() && 
          x.LastName == recordFromPhoneLog.LastName.ToLower() 
          select x).ToList(); 


      var samePhone = (from x in db.IdentityNumbers 
          where x.TenDigitNumber == recordFromPhoneLog.Phone 
          select x).ToList(); 

      //going to add the people to this list in the for loop once I can figure out how to access the entityID 
      var people = new List<IdentityPersonName>(); 
      for (int i = 0; i < samePhone.Count; i++) 
      { 
       //get the numberID of the current phone number 
       var numID = samePhone[i].NumberID; 
       //get entityID of that number 
       var entID = (from x in person 
          where x.//this is where I am stuck and cannot figure out how to get the entityID from the join table based on the numberID 

          ); 
      } 



      ViewBag.FN = recordFromPhoneLog.FirstName; 
      ViewBag.LN = recordFromPhoneLog.LastName; 
      return View(sameName); 
     } 
    } 
} 
+0

我不知道爲什麼你正在使用LINQ如果你正在使用EF查詢您的數據。你以前曾經使用EF嗎?如果不考慮首先閱讀[EF文檔](https://msdn.microsoft.com/en-us/data/ee712907)。 –

回答

1

簡單:

var entities = db.IdentityEntities.Where(m => m.IdentityNumbers.Any(n => n.NumberID == numberID)); 

總之,這種選擇任何IdentityEntity實例有一個相關的IdentityNumber實例id爲numberID

+0

更進一步和這樣做:'VAR entitiesWithSamePhone = db.IdentityEntities.Where(M => m.IdentityNumbers.Any(N => n.TenDigitNumber == recordFromPhoneLog.Phone));' – brown1455

0
var entID = from x in person.where(s=>s.**numID**==numID) 
      select x.**entityid**; 

小心爲VAR entID它可以像一個單可變或作爲根據多變量進行處理查詢的結果。 我希望它有幫助。

+0

感謝您的迴應,但我在嘗試代碼時收到此錯誤消息:te 錯誤\t CS1061 \t'IdentityEntity'不包含'numID'的定義,也沒有接受第一個參數類型的擴展方法'numID' 'IdentityEntity' 可以找到(是否缺少using指令或程序集引用?)\t CaseManagement \t H:\ VisualStudioProjects \ CaseManagement \ CaseManagement \ CaseManagement \ \控制器IdentityPersonSelectController.cs 活動 – brown1455

+0

星星,你應該之間的變量請使用您的變量名稱,而不是我的現在請檢查它。 – yazan