2016-06-24 11 views
0

私は多対多リレーションシップの最初のプロジェクトを行っていますが、まだasp.netで初心者です。基本的な質問は許してください。私が取り組んでいるプロジェクトには多くのエンティティ(人、ビジネスなど)があり、各エンティティには多くの電話番号(家、仕事、セルなど)を持てます。私は電話番号検索をしなければなりません。電話番号検索は、その電話番号が関連付けられているすべてのエンティティ(この例では人のみ)を返します。私は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

EFを使用している場合、なぜデータを照会するのにLINQを使用しているのか分かりません。前にEFで働いたことがありますか?もしそうでなければ、最初に[EF Docs](https://msdn.microsoft.com/en-us/data/ee712907)を読むことを検討してください。 –

答えて

1

単に:要するに

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

、これはID numberIDある関連IdentityNumberインスタンスを有する任意IdentityEntityインスタンスを選択します。

+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

関連する問題