2012-02-09 78 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