1

So I have an object that is part of a reply from a web request that has two properties on it. Both are string arrays. The first one is called COLUMNNAMES and the second is called VALUES. For instance COLUMNNAMES would have the following in it:

"ID", "NAME", "DATEADDED", "DATECHANGED"

and VALUES would have the following in it:

"1", "APPLES", "09/21/2013", "09/21/2013"

"2", "ORANGES", "09/21/2013", "09/21/2013"

"3", "STRAWBERRIES", "09/21/2013", "09/21/2013"

What I want to be able to do is somehow process these into a sort of data structure that I can call like so:

Dim rows = GetReply()

For Each r in rows
    Dim value = r("ID") 'ID is a column name
Next 

Is there a type that I can implement to create this or does this already exist? Thanks!

4

2 回答 2

1

这里最好的答案是数据表。

于 2015-06-23T15:38:16.497 回答
0

您可以使用 LINQ 将其转换为Dictionarys 序列:

class Reply {
    string[] _columnNames = {"ID", "NAME"}; // the list of columns you received

    string[][] _rowValues = { // the lists of values you received
        new[]{"1", "APPLES"},
        new[]{"2", "ORANGE"}
    };

    public IEnumerable<Dictionary<string, string>> Rows {
        get {
            foreach (var row in _rowValues) {
                yield return _columnNames
                                .Zip(row, (key, value) => new {key, value})
                                .ToDictionary(kv=>kv.key, kv=>kv.value);
            }
        }
    }
}

// ...

foreach (row in new Reply().Rows) {
    var id = row["ID"];
    var name = row["NAME"];
}
于 2013-09-21T14:51:29.710 回答