2016-08-02 129 views
0

我在控制器中有以下代碼,正如你所看到的,它返回一個序列化爲Json的對象。將Json結果合併爲一個

... 
    [HttpGet("{id}")] 
      public IActionResult Get(string id) 
      { 
       ClientsRepository ClientsRepo = new ClientsRepository(connectionString); 
       return Json(ClientsRepo.GetClientCreditSummary(id)); 
      } 
... 

那裏得到的數據的方法是在ClientsRepo.GetClientCreditSummary,我想用另一個叫ClientsRepo.GetClient合併它,並返回它的JSON結果在這同一個控制器的動作。

我該怎麼做?

+0

通過合併究竟是什麼意思呢?這兩個數據是同一類型的嗎?你想要什麼樣的結構? –

回答

0

把這樣兩個對象合併成一個對象並不是很好的形式。

請考慮改用使得持有其他兩個爲屬性的新對象:

[HttpGet("{id}")] 
public IActionResult Get(string id 
{ 
    var repo = new ClientsRepository(connectionString); 
    var creditSummary = repo.GetClientCreditSummary(id); 

    var client = repo.GetClientById(id); 

    var result = new 
    { 
     Client = client, 
     CreditSummary = creditSummary 
    }; 

    return Json(result); 
} 
相關問題