2013-11-28 84 views
0

開始我有一個表結構類似LINQ的(遞歸與否)發現所有的兒童,從父

Application_OID 
Status 
{some other fields} 
RecommendedFromApplication_OID 

的RecommendedFromApplication_OID將包含來自上次推薦的對象Application_OID。例如,有一個應用程序

Application_OID = 1 
Status = New 
{some other fields} 
RecommendedFromApplication_OID = null 

此應用程序,建議另一份工作,另一個應用程序將具有以下值

Application_OID = 2 
Status = New 
{some other fields} 
RecommendedFromApplication_OID = 1 

和第一對象的狀態創建將更新到

Application_OID = 1 
Status = Recommended 
{some other fields} 
RecommendedFromApplication_OID = null 

比方說,我建議新創建的另一項工作,將創建一個新的條目以下值

Application_OID = 3 
Status = New 
{some other fields} 
RecommendedFromApplication_OID = 2 

而第二應用將被更新以推薦,最終的數據將是

Application_OID = 1 
Status = Recommended 
{some other fields} 
RecommendedFromApplication_OID = null 

Application_OID = 2 
Status = Recommended 
{some other fields} 
RecommendedFromApplication_OID = 1 

Application_OID = 3 
Status = New 
{some other fields} 
RecommendedFromApplication_OID = 2 

這樣,我就能夠推薦很多次我想申請。

現在的問題;我想寫在LINQ的功能,這將需要一個Application_OID,並會返回一個包含所有的ID列表中的建議(子)應用程序的Application_OID,像

private List<int> GetRecommendedIds(int id) 
{ 
    var applicationIds = (/*some query*/).ToList(); 
    return applicationIds; 
} 

然後調用

GetRecommendedIds(1) 

將其值

2,3 
+0

這就是爲什麼這樣的表結構是最糟糕的選擇返回一個int列表,如果您打算執行「從父級獲取所有層次結構」等查詢。你可以改變它嗎? – Dennis

+0

我恐怕不行,我們有大量的生產數據。 –

+0

假設你有另一個應用程序'''''RecommendedFrom'2',另一個'A5R1','A6R5'和'A7R5'。您的預期結果是「2,3,4,5,6,7」(深度優先)還是「2,5,3,4,6,7」(先呼吸),還是實際順序不重要? – Corak

回答

0
public List<int> GetRecommendedForApplication(int Application_OID) 
    { 
     var recommendedApplications = new List<int>(); 
     var context = new SomeEntities(); 
     int applicationId = Application_OID; 
     Application application; 
     do 
     { 
      application = (from applications in context.Applications 
          join recommendedApplication in context.Applications 
           on applications.Application_OID equals 
           recommendedApplication.ParentApplication_OID 
           where recommendedApplication.ParentApplication_OID == applicationId 
          select recommendedApplication.Application_OID).FirstOrDefault(); 
      if (application != null) 
      { 
       recommendedApplications.Add(application); 
       applicationId = application.Application_OID; 
      } 
     } while (application != null && application.ParentApplication_OID != null); 

     return recommendedApplications; 
    }