获取属性名称,只需要检索某些列

发布于 2025-01-04 11:49:42 字数 2971 浏览 0 评论 0原文

答案摘要:

使用下面 Jon Skeet 的答案解决了这个问题。这是完成的代码

    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;
    }

Answer Summary:

Solved this problem using Jon Skeet's answer below. Here is the finished code

    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;
    }

Question

I am building my SQL query dynamically from user input, and then exporting the results to a CSV file. I have a class called RegDataDisplay which has a property for each of the possible columns returned by my query. I can tell what columns are being selected but in my CSV creator I need to be able to only output those specific columns.

In the example below, all of the data I have retrieved is in rList, and the names of the properties I need are in selectors. So I want to iterate through the list and then add only the properties I need to my CSV data.

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]]);

        } 
    }
}

Thanks

EDIT: On the right track now but I'm coming up against an error "Object does not match target type".

    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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趴在窗边数星星i 2025-01-11 11:49:42

假设您使用的是 .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);
    }
}

Assuming you're on .NET 3.5 or higher, it sounds like you may want something like:

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文