2016-06-07 86 views
0

我將從我的sqlserver存儲過程調用中獲取我的近似數據表結構。我正在使用.netframework 3.5,我想使用ado.net將此數據錶轉換爲json輸出。我堅持在GetCountryList(FK,TypeName)。請幫助我實現下面的json輸出。感謝您的幫助提前。c#:如何使用c#方法與參數調用泛型方法?

Type  ID Name   FK 

Continent 1 America   0 
Continent 2 Asia   0 
Continent 3 Africa   0 
Country  11 USA    1 
Country  12 China   2 
Country  13 India   2 
Country  14 Kenya   3 

DataEntity

public class UserData 
{ 
    public string Type { get; set; } 
    public string ID { get; set; } 
    public string Name { get; set; } 
    public string FK {get; set;} 
} 

服務方法

​​

}

預期JSON輸出:

"data": { 
    "Contnient": 
     [ 
      { "Id": "1", "Type": "Contient", "Name" :"America","FK":"1" }, 
      { "Id": "2", "Type": "Contient", "Name" :"Asia", "FK":"2" }, 
      { "Id": "3", "Type": "Contient", "Name" :"Africa", "FK":"2" }, 
      { "Id": "4", "Type": "Contient", "Name" :"Asia", "FK":"2" } 
     ], 

    "America": 
     { 
      "Country": 
      [ 
       { "Id": "11", "Type": "Country","Name":"India","FK":"1" } 

      ] 
     }, 
    "Asia: 
     { 
     "Country": 
       [ { "Id": "12", "Type": "Country","Name":"China","FK":"2" }, 
       { "Id": "13", "Type": "Country","Name":"India","FK":"2" } 
       ] 
     } 
    "Africa": 
     { 
     "Country": 
       [ { "Id": "14", "Type": "Country","Name":"Kenya","FK":"3" } 
       ] 

    } 
+1

這是什麼都與'調用泛型方法與parameters'辦? –

+0

我用通用方法實現更新了我的問題。但我不確定,如何在我的代碼中調用泛型方法。這種通用方法的原因是爲了獲得每個大洲的國家名單。你可以參考我對json輸出的期望。 – user6410893

+0

@EricJ。我已經更新了預期的JSON輸出。 – user6410893

回答

1

不應該泛型函數被定義爲:

private static List<T> GetCountryList<T>(lstCountry,ForgnKey,TypeName) 
{ 
     return lstCountry.Where(p=>p.FK==ForgnKey).ToList(); 
} 

然後稱爲:

var CountriesList = GetCountryList<UserData>(lstCountry,FK,TypeName);