RowTable到Json序列化的KeyValue列表

时间:2018-01-16 17:52:55

标签: c# linq

我有一些从Mysql查询返回的数据,我使用Linq进行分页并需要返回一个键值对,以便稍后将其嵌套在另一个List / Dictionary中,然后将其序列化为JSON。我意识到这应该是一个重复的问题,但我找不到任何适合我的解决方案。

我的初始数据源代码如下所示:

var pageData = rawData.AsEnumerable().Skip(currentIndex * Config.PageSize)
    .Take(Config.PageSize);

我需要迭代并将每个转换为键值。诀窍是它每次都会有不同的结构,所以我不能硬编码列名。例如,在序列化后,它可能如下所示:

{
  "reference_id": 19,
  "category": 2,
  "title": "Unconfirmed",
  "code": "Unconfrm"
}

或者像这样:

{
  "reference_id": 11,
  "last_name": "Smith",
  "first_name": "John",
  "pref_name": "",
  "status": 4,
  "gender": 0
}

我有这个创建键值对,但它也将它添加为Data的字典引用,而不是仅添加另一个项目到数组/列表。

var rows = new List<RowDto>();
foreach (var row in pageData)
{



    var dictionary = new Dictionary<string, string>();
    for (int index = 0; index < row.ItemArray.Length; index++)
    {
        dictionary.Add(row.GetColumn(index), row.ItemArray[index].ToString());
    }

    rowDto.Data = dictionary;
    rows.Add(rowDto);
}
public class RowDto
{
    public Dictionary<string, string> Data { get; set; }
}

我意识到在课程RowDto中我将其定义为字典,但我无法弄清楚如何将其切换到List。每次我做VS2017抱怨它不能转换(字符串,字符串)到int / string []或我尝试过的任何其他东西。

0 个答案:

没有答案