LINQ字典到锯齿状数组?

时间:2011-05-16 09:57:04

标签: c# arrays linq

有一个返回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;

5 个答案:

答案 0 :(得分:3)

在查询中创建一个字符串数组而不是一个对象,然后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();
}

答案 1 :(得分:3)

select中,您需要创建一个字符串数组(new [])。在您的示例中,您创建了一个新的anonymous type

public string[][] GetRecordFields(string selectedRecord)
{
    string[][] recordFields = (from record in _recordMasterList
                        where record.Key == selectedRecord
                        select new []
                        {
                            record.Key,
                            record.Value
                        }).ToArray();

    return recordFields;
}

(我稍微更改了代码以处理类型_recordMasterList的{​​{1}}。此外,在这样的代码中,我发现明确声明我的变量类型更清楚,而不是依赖于{ {3}}。也就是说,对于数组,我更喜欢使用implicit typing - Dictionary<string, string>而不是new []。)

答案 2 :(得分: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;
}

答案 3 :(得分: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;
}

答案 4 :(得分:0)

具有相反尺寸的更通用的版本:

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