2012-02-09 79 views
0

答摘要:獲取屬性名稱,需要檢索只有某些列

解決使用下面喬恩斯基特的回答這個問題。下面是最終的程序代碼

public static CSVData CreateCSVData(List<RegDataDisplay> rList, 
                 string[] selectors) 
    { 
     CSVData csv = new CSVData(); // Create the CSVData object 
     foreach(string selector in selectors) 
     { 
      // Get the PropertyInfo for the property whose name 
      // is the value of selector 
      var property = typeof(RegDataDisplay).GetProperty(selector); 

      // Use LINQ to get a list of the values for the specified property 
      // of each RegDataDisplay object in the supplied list. 
      var values = rList.Select(row => property.GetValue(row, null) 
           .ToString()); 

      // Create a new list with the property name to use as a header 
      List<string> templs = new List<string>(){selector}; 

      // Add the returned values after the header 
      templs.AddRange(values); 

      // Add this list as a column for the CSVData object. 
      csv.Columns.Add(templs); 
     } 
     return csv; 
    } 

問題

我動態構建我的SQL查詢來自用戶的輸入,然後將結果導出爲CSV文件。我有一個名爲RegDataDisplay的類,它對我的​​查詢返回的每個可能列都有一個屬性。我可以告訴哪些列正在被選中,但在我的CSV創建者中,我只需要能夠輸出這些特定的列。

在下面的例子中,我檢索到的所有數據都在rList中,我需要的屬性名稱在選擇器中。所以我想遍歷列表,然後只添加我需要的屬性給我的CSV數據。

public static CSVData CreateCSVData(List<RegDataDisplay> rList, string[] selectors) 
{ 
    CSVData csv = new CSVData(); 
    for(int i = 0; i < selectors.Length; i++) 
    { 
     csv.Columns.Add(new List<string>(){selectors[i]}); 
    } 

    // So now I have the headers for the CSV columns, 
    // I need the specific properties only which is where I'm stuck 

    for(int i = 0; i < selectors.Length; i++) 
    { 
     for(int j = 0; j < rList.Count; j++) 
     { 
      // If it was javascript I would do something like this 

      csv.Columns[i].Add(rList[j][selectors[i]]); 

     } 
    } 
} 

感謝

編輯:在現在走上了正軌,但我來面對一個錯誤「對象不匹配目標類型」。

public static CSVData CreateCSVData() 
    { 
     // I've created a test method with test data 

     string[] selectors = new string[] { "Firstname", "Lastname" }; 
     List<RegDataDisplay> rList = new List<RegDataDisplay>(); 
     RegDataDisplay rd = new RegDataDisplay(); 
     rd.Firstname = "first"; 
     rd.Lastname = "last"; 
     rList.Add(rd); 

     CSVData csv = new CSVData(); 
     foreach(string selector in selectors) 
     { 
      var property = typeof(RegDataDisplay).GetProperty(selector); 
      var values = rList.Select(row => property.GetValue(rList, null).ToString()) 
           .ToList(); // Error throws here 
      csv.Columns.Add(values); 
     } 
     return csv; 
    } 

回答

2

假設你在.NET 3.5或更高版本,這聽起來像你可能想是這樣的:

public static CSVData CreateCSVData(List<RegDataDisplay> rList, 
            string[] selectors) 
{ 
    CSVData csv = new CSVData(); 
    foreach (string selector in selectors) 
    { 
     var prop = typeof(RegDataDisplay).GetProperty(selector); 
     var values = rList.Select(row => (string) prop.GetValue(row, null)) 
          .ToList(); 
     csv.Columns.Add(values); 
    } 
} 
+0

這看起來不錯,但我無法得到它的工作。如果我單步執行代碼,並查看值變量。它有一個結果視圖,當展開時顯示「對象與目標類型不匹配」。我改變了foreach到一個for循環,然後使用「string s =(string)property.GetValue(rlist [0],null)」並且第一行的值爲罰款。但是這樣我就不得不把它放到兩個循環中,我真的很喜歡你的單一循環。 – 2012-02-09 21:01:31

+0

@JamesHay:你的一些屬性實際上不是字符串嗎?好的,會編輯。 – 2012-02-09 21:18:43

+0

我編輯了我的問題以顯示我正在嘗試的最近的測試方法。所有的屬性都是字符串。 – 2012-02-09 21:28:34