2017-06-21 105 views
2

TLDR:我試圖從用戶顯示的引用,用戶B,我想用戶B要能夠認識有一個從用戶A參考。如何創建交叉引用檢查?

在我的申請,我在那裏我連接用戶彼此才能看到它們之間的關係(例如,兄弟,姐妹等)這一部分被稱爲案例交叉參考的部分。

我與他們案例GUID連接的用戶。

如果我想連接用戶A用戶B,它只是把我是從爲From_Case連接用戶和其他用戶爲To_Case

Cross Reference table

現狀

我目前能夠顯示從用戶A用戶B,反之亦然,但連接時,我去用戶B,它並沒有顯示正確的案件編號關係

ToCases = await db.CaseCrossReference 
        .Include(x => x.Relationship) 
        .Include(x => x.Type) 
        .Include(x => x.Case2.CaseNumber) 
        .Where(x => (x.From_Case == cid || x.To_Case == cid) 
           && x.Deleted == false) 
        .Select(o => new CaseCrossReferenceToRelationshipViewModel() 
        { 
         CaseIdFrom = o.From_Case, 
         CaseIdTo = o.To_Case, 
         CaseCrossReferenceId = o.CaseCrossReferenceId, 
         CaseNumber = o.Case2.CaseNumber == null ? null : o.Case2.CaseNumber.CaseNumberPrefix + "-" + o.Case2.CaseNumber.Number, 
         LivingTogether = o.LivingTogether, 
         Split = o.Split, 
         SplitDate = o.SplitDate, 
         SplitReason = o.SplitReason, 
         Type = o.Type.Definition, 
         RelationshipDefinition = o.Relationship.Definition 
        }).ToListAsync() 


foreach (var cases in model.ToCases) 
      { 
        // Check for any reverse relationships 
        var reference = await db.CaseCrossReference.Include(x => x.Relationship).Include(x => x.Case1.CaseNumber).FirstOrDefaultAsync(m => m.From_Case == cases.CaseIdFrom && m.To_Case == cases.CaseIdTo); 
        if (reference != null) 
         { 
          if (cases.Gender == "M") 
          { 
           cases.RelationshipDefinition = await relationshipsdd.Where(r => r.Id == reference.Relationship.Id).Select(r => r.MaleReverseRelationshipDefinition).FirstOrDefaultAsync(); 
           cases.CaseCrossReferenceId = reference.CaseCrossReferenceId; 
           cases.CaseNumber = reference.Case1.CaseNumber?.Number == null ? null : reference.Case1.CaseNumber.CaseNumberPrefix + "-" + reference.Case1.CaseNumber.Number; 
          } 
          else 
          { 
           cases.RelationshipDefinition = await relationshipsdd.Where(r => r.Id == reference.Relationship.Id).Select(r => r.FemaleReverseRelationshipDefinition).FirstOrDefaultAsync(); 
           cases.CaseCrossReferenceId = reference.CaseCrossReferenceId; 
           cases.CaseNumber = reference.Case1.CaseNumber?.Number == null ? null : reference.Case1.CaseNumber.CaseNumberPrefix + "-" + reference.Case1.CaseNumber.Number; 
          } 

         } 
      } 

你可能不知道什麼是CID。這是CaseID /案例GUID查詢字符串值。當我在用戶A是,該CID是用戶A GUID,當我去用戶B它的用戶B GUID。只要它是目前案件ID。

我需要能夠做到以下幾點:

無論在哪個用戶我在,我應該能夠看到其他用戶(S)的連接。如果我在用戶B我,那麼我就應該能看到,我要用戶A,反之亦然連接。

下面的圖片展示瞭如我在PS-100002,我可以看到我有連接PS-100000nm正確的情況。 Reference PS-100002

但如果去PS-100000nm,我仍然看到PS-100000nm作爲我的參考。它需要是PS-100002與正確的關係(父親)。

Reference PS-100000

能否請你幫我找到這個問題的妥善解決辦法?

專用房間這個話題:https://chat.stackexchange.com/rooms/60854/room-for-forrest-and-kevin-maxwell

+0

凱文,嘗試定義什麼PS-XXXX代表或用易於識別的術語取代它們。如果他們每個都代表案例,可以命名爲案例。 – Sometowngeek

回答

2

我基本上不得不扭轉成員之間的關係。所以我想出了以下解決方案:

var member = await db.Member.Include(x => x.Gender).Include(x => x.Case.CaseNumber) 
      .FirstOrDefaultAsync(x => x.Relationship.Code == "PA" && x.CaseId == cid && x.Deleted == false); 
     var relationshipsdd = db.RelationshipDD; 

     var model = new CaseCrossReferenceFromRelationshipViewModel() 
     { 
      MemberId = member?.MemberId, 
      FirstName = member?.FirstName, 
      MiddleName = member?.MiddleName, 
      LastName = member?.LastName, 
      Gender = member?.Gender.Code, 
      CaseNumber = member?.Case.CaseNumber?.Number == null 
       ? null 
       : member.Case.CaseNumber?.CaseNumberPrefix + "-" + member.Case.CaseNumber?.Number, 
      CaseId = member?.CaseId, 


      ToCases = await db.CaseCrossReference 
       .Where(x => (x.From_Case == cid || x.To_Case == cid) 
          && x.Deleted == false) 
       .Select(o => new CaseCrossReferenceToRelationshipViewModel() 
       { 
        CaseIdFrom = o.From_Case, 
        CaseIdTo = o.To_Case, 
        CaseCrossReferenceId = o.CaseCrossReferenceId, 
        LivingTogether = o.LivingTogether, 
        Gender = o.Relationship.Gender.Code, 
        Split = o.Split, 
        SplitDate = o.SplitDate, 
        SplitReason = o.SplitReason, 
        Type = o.Type.Definition, 
        RelationshipDefinition = o.Relationship.Definition, 
        Relationship = o.Relationship.Id 
       }).ToListAsync() 
     }; 


     foreach (var cases in model.ToCases) 
     { 
      // Check for any reverse relationships/Case Number 
      var reference = await db.CaseCrossReference.Include(x => x.Relationship) 
       .Include(x => x.Case2.CaseNumber).Include(x => x.Case1.CaseNumber) 
       .FirstOrDefaultAsync(m => m.From_Case == cases.CaseIdFrom && m.To_Case == cases.CaseIdTo); 

      if (cases.CaseIdFrom == cid) 
      { 
       member = await db.Member.Include(x => x.Gender) 
        .FirstOrDefaultAsync(x => x.Relationship.Code == "PA" && x.CaseId == cases.CaseIdTo && 
               x.Deleted == false); 

       cases.CaseIdFrom = cases.CaseIdTo; 
       cases.CaseNumber = reference.Case2.CaseNumber?.Number == null 
        ? null 
        : reference.Case2.CaseNumber.CaseNumberPrefix + "-" + reference.Case2.CaseNumber.Number; 
       cases.FirstName = member.FirstName; 
       cases.MiddleName = member.MiddleName; 
       cases.LastName = member.LastName; 

       if (member.Gender.Code == "M") 
       { 
        cases.RelationshipDefinition = await relationshipsdd 
         .Where(r => r.Id == reference.Relationship.Id) 
         .Select(r => r.MaleReverseRelationshipDefinition).FirstOrDefaultAsync(); 
       } 
       else 
       { 
        cases.RelationshipDefinition = await relationshipsdd 
         .Where(r => r.Id == reference.Relationship.Id) 
         .Select(r => r.FemaleReverseRelationshipDefinition).FirstOrDefaultAsync(); 
       } 
      } 
      else 
      { 
       member = await db.Member.Include(x => x.Gender) 
        .FirstOrDefaultAsync(x => x.Relationship.Code == "PA" && x.CaseId == cases.CaseIdFrom && 
               x.Deleted == false); 
       cases.CaseIdTo = cases.CaseIdFrom; 
       cases.CaseNumber = reference.Case1.CaseNumber?.Number == null 
        ? null 
        : reference.Case1.CaseNumber.CaseNumberPrefix + "-" + reference.Case1.CaseNumber.Number; 
       cases.FirstName = member.FirstName; 
       cases.MiddleName = member.MiddleName; 
       cases.LastName = member.LastName; 
      } 
     } 


     return View(model); 
1

只是不要.ToList()的第一個查詢。一旦使用ToList()獲取結果,您將不得不使用Lazy Loading來導航導航屬性。但是,如果它在一個查詢中,則投影ViewModel類型的列將在單個SQL查詢中獲取。

ToCases  = await db.CaseCrossReference 
         .Include(x => x.Relationship) 
         .Include(x => x.Type) 
         .Where(x => (x.From_Case == cid || x.To_Case == cid) 
             && x.Deleted == false) 
         .Select(o => new CaseCrossReferenceToRelationshipViewModel() 
       { 
        CaseId = o.From_Case, 
        CaseCrossReferenceId = o.CaseCrossReferenceId, 
        CaseNumber = o.Case2.CaseNumber?.Number == null ? null : o.Case2.CaseNumber.CaseNumberPrefix + "-" + o.Case2.CaseNumber.Number, 
        LivingTogether = o.LivingTogether, 
        Split = o.Split, 
        SplitDate = o.SplitDate, 
        SplitReason = o.SplitReason, 
        Type = o.Type.Definition, 
        RelationshipDefinition = o.Relationship.Definition 

       }).ToListAsync(); 
+0

謝謝。根據你的建議,我改變了我的查詢。結果是相同的,但我認爲你的查詢會提高性能,對吧? –

+1

是的,性能會更好。那麼,實際問題是什麼? –

+0

嘗試創建簡化的模型和repro,包括插入實體成員和預期結果。如果可以的話,張貼。 –