2016-06-07 49 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);