将LINQ字典转换为交错数组?

6

有一种方法返回2D数组,该方法从LINQ查询中查询字典,并尝试将键和值存储在2D数组中。

但是我无法做到这一点。

public string[][] GetRecordFields(string selectedRecord)
    {

        var recordFields = (from record in _recordMasterList
                            where record.Item1 == selectedRecord
                            select new 
                            {
                                record.Item2.Keys,
                                record.Item2.Values
                            }).ToArray();
      return recordFields;       
  }

但它失败了,有什么办法吗?
编辑: _recordMasterList 的类型。
List<Tuple<string, Dictionary<string, string>>> _recordMasterList;

你能否也包含一下 Tuple<> 的定义? - Richard Ev
5个回答

5

在查询中创建一个字符串数组而不是对象,然后 ToArray 将返回一个数组的数组:

public string[][] GetRecordFields(string selectedRecord) {
  return (
    from record in _recordMasterList
    where record.Item1 == selectedRecord
    select new string[] {
      record.Item2.Keys,
      record.Item2.Values
    }
  ).ToArray();
}

我尝试了这个方法,但它返回的是一个二维数组,其中第一维包含键和值,而第二维为空...就像string([keys][values],[]); 但我想要的是像string([keys],[values])这样的格式。 - PawanS
@GAPS:在多维数组中,所有数据都在同一维度中,因此使用二维数组无法实现您想要的功能。如果您希望以其他方式实现,请具体说明您的意思。 - Guffa
@GAPS - 你的意思是希望结果数组在相反的轴上进行索引吗?也就是说:string[2, 50] 而不是 string[50, 2]? - Buh Buh
@ Buh Buh 不...我只想简单地处理。我有一个 List<Tuple<string, Dictionary<string, string>>>,所以我想把所有字典的键和值存储在一个二维数组中。这里的 List 可能有多个 Tuples,每个 Tuple 都有一个 Dictionary,所以我想获取所有字典的键和值。 - PawanS

5
在你的select中,你需要创建一个字符串数组(new[])。在你的示例中,你正在创建一个新的匿名类型
public string[][] GetRecordFields(string selectedRecord)
{
    string[][] recordFields = (from record in _recordMasterList
                        where record.Key == selectedRecord
                        select new []
                        {
                            record.Key,
                            record.Value
                        }).ToArray();

    return recordFields;
}

我稍微修改了代码,以处理类型为Dictionary<string, string>_recordMasterList。此外,在这样的代码中,我发现显式声明变量类型比依赖隐式类型更清晰。尽管如此,对于数组,我更喜欢使用隐式数组类型 - 使用new []而不是new string[]


是的,但它返回的是字符串([[key][value]],[])... 我想要的是字符串([keys],[values])。 - PawanS

3

这不是一个一行代码的LINQ魔法,但以下是代码:

/// <summary>
/// Converts dictionary to 2d string array
/// </summary>
/// <param name="Dictionary">Dictionary to be converted</param>
/// <returns>2D string Array</returns>
private string[,] ConvertDictionaryTo2dStringArray(Dictionary<string, string> Dictionary)
{
    string[,] stringArray2d = new string[2, Dictionary.Count];
    int i = 0;

    foreach (KeyValuePair<string, string> item in Dictionary)
    {
        stringArray2d[0, i] = item.Key;
        stringArray2d[1, i] = item.Value;
        i++;
    }

    return stringArray2d;
}

1

一个更通用的版本,具有反向尺寸:

private object[,] Dictionary2Array(Dictionary<object, object> dic)
{
    object[,] arr = new object[dic.Count, 2];
    int i = 0;

    foreach (KeyValuePair<object, object> item in dic)
    {
        arr[i, 0] = item.Key;
        arr[i, 1] = item.Value;
        i++;
    }

    return arr;
}

0

你的问题还有点令人困惑。这是你要寻找的行为吗?

(我知道这个答案可以进行很多优化,但似乎这是弄清楚你想要什么的最简单方法。)

public string[,] GetRecordFields(string selectedRecord)
{
    //List<Tuple<string, Dictionary<string, string>>> _recordMasterList;

    List<Dictionary<string, string>> selectedRecords
        = (from record in _recordMasterList
            where record.Item1 == selectedRecord
            select record.Item2)
            .ToList();

    int totalNumberOfRecords = 0;

    foreach(Dictionary<string, string> d in selectedRecords)
    {
        totalNumberOfRecords += d.Count();
    }

    string[,] result = new string[2, totalNumberOfRecords];

    int i = 0;
    foreach(Dictionary<string, string> d in selectedRecords)
    {
        foreach(KeyValuePair<string, string> kvp in d)
        {
            result[0,i] = kvp.Key;
            result[1,i] = kvp.Value;
            ii++;
        }
    }

    return result;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接