2015-11-03 70 views
0

我想基於一個公共id加入到兩個類中,這個id從父表和兩個子表中使用lambda/linq生成一行。我在下面粘貼了,到目前爲止我嘗試過的,但是我得到的是每行10行,這是不正確的。歡迎任何對代碼的建議或修改。使用LINQ加入基於公共id的兩個類LAMBDA

namespace x 
{ 
    public class IntegrationSearchResult 
    { 
     public string Dfe { get; set; } 
     public SchoolResult SchoolSearchResult { get; set; } 
     public List<PupilResult> PupilSearchResult { get; set; } 
    } 

    public class SchoolResult 
    { 
     public int SupplierId { get; set; } 
     public string SchoolName { get; set; }    
     public DateTime CreateDate { get; set; } 
     public int Pupils { get; set; }  
    } 

    public class PupilResult 
    { 
     public int SupplierId { get; set; }  
     public string FirstName { get; set; } 
     public string LastName { get; set; }   
    } 
} 

功能

private Func<IEnumerable<IntegrationSearchResult>> _getAllDummySearchResults; 
private Func<string, IntegrationSearchResult> _getDfeDummySearchSchool; 

我試圖達到的結果

_

getAllDummySearchResults =() => (from dummySupplierId in Enumerable.Range(1, 10)              
    select new IntegrationSearchResult 
    { 
     Dfe = "School-1-" + dummySupplierId.ToString("000000"), 

     SchoolSearchResult = new SchoolResult 
     { 
      SupplierId = dummySupplierId, 
      SchoolName = "SchoolName" + dummySupplierId.ToString("000000"),   
      CreateDate = new DateTime(), 
      Pupils = 123    
     } 
     , 
     PupilSearchResult = new List<PupilResult> 
     { 
      new PupilResult 
      { 
       SupplierId = dummySupplierId,      
       FirstName = "FirstName - Pupil-1-" + dummySupplierId.ToString("000000"), 
       LastName = "LastName - Pupil-1-" + dummySupplierId.ToString("000000") 
      }, 
      new PupilResult 
      { 
       SupplierId = dummySupplierId, 
       FirstName = "FirstName - Pupil-2-" + dummySupplierId.ToString("000000"), 
       LastName = "LastName - Pupil-2-" + dummySupplierId.ToString("000000") 
      } 
     } 
    }).ToList(); 

    _getDfeDummySearchSchool = dfe => 
    { 
     var allSchools = _getAllDummySearchResults(); 
     return allSchools.FirstOrDefault(x => x.Dfe == dfe); 
    }; 

結果預計:學校信息搜索結果應該顯示在e行,它是相應的學生,它是兩行具有相同的供應商ID。

{ 
    "Dfe": 0, 
    "SchoolSearchResult": [ 
    { 
     "SupplierId": 1, 
     "SchoolName": "SchoolName000001", 
     "CreateDate": "0001-01-01T00:00:00", 
     "Pupils": 123 
    }], 
    "PupilSearchResult": [ 
    { 
     "SupplierId": 1, 
     "FirstName": "FirstName - Pupil-1-000001", 
     "LastName": "LastName - Pupil-1-000001" 
    }, 
    { 
     "SupplierId": 1, 
     "FirstName": "FirstName - Pupil-1-000002", 
     "LastName": "LastName - Pupil-1-000002" 
    }] 
    } 

回答

0

您可以第一個方法更改定義你以下

getAllDummySearchResults =() => (from dummySupplierId in Enumerable.Range(1, 10)              
    select new IntegrationSearchResult 
    { 
     Dfe = "School-1-" + dummySupplierId.ToString("000000"), 

     SchoolSearchResult = new SchoolResult 
     { 
      SupplierId = dummySupplierId, 
      SchoolName = "SchoolName" + dummySupplierId.ToString("000000"),   
      CreateDate = new DateTime(), 
      Pupils = 123    
     } 
     , 
     PupilSearchResult = new List<PupilResult> 
     { 
      new PupilResult 
      { 
       SupplierId = dummySupplierId,      
       FirstName = "FirstName - Pupil-1-" + dummySupplierId.ToString("000000"), 
       LastName = "LastName - Pupil-1-" + dummySupplierId.ToString("000000") 
      }, 
      new PupilResult 
      { 
       SupplierId = dummySupplierId, 
       FirstName = "FirstName - Pupil-2-" + dummySupplierId.ToString("000000"), 
       LastName = "LastName - Pupil-2-" + dummySupplierId.ToString("000000") 
      } 
     } 
    }).Take(1).ToList(); 

它只會從子集選擇兩個記錄,當你調用平等的第二種方法,它會調用第一種方法從子類中獲取兩行。

+0

您可以在選擇兩行之前對內部集合進行排序 –

+0

感謝您的回覆,但我不想限制結果2.我希望它應該返回可能是從父表(SchoolResult)的10行,並匹配子表的行(PupilResult)。有了這個,我仍然從父表中得到2行,從子表中得到2行有供應商ID 1,2的錯誤。它應該返回具有相同供應商ID的2行。謝謝 – DotNetGeek

+0

我改變了'拿(2)'到'拿(1)'。它會得到你想要的,但我不知道你正在試圖通過這樣的查詢達到什麼目的。如果你可以把你的用例放在一個更好的位置來回答你的問題 –

相關問題