【问题标题】:LINQ dictionary to jagged array?LINQ字典到锯齿状数组?
【发布时间】:2011-05-16 09:57:03
【问题描述】:

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

但我做不到

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&lt;&gt;的定义?

标签: c# arrays linq


【解决方案1】:

在查询中创建一个字符串数组而不是一个对象,那么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 包含键和值,维度 2 不包含任何内容.... 像 string([keys][values],[]);但我想要像 string([keys],[values])
  • @GAPS:在多维数组中,所有数据都在同一维中,因此您想要的二维数组是不可能的。如果你想用其他方式,你必须说明你的意思。
  • @GAPS - 你的意思是你想让结果数组在相反的轴上被索引吗?如: string[2, 50] 而不是 string[50, 2] ?
  • @ Buh Buh 不...我只想以简单的方式。我有一个List&lt;Tuple&lt;string, Dictionary&lt;string, string&gt;&gt;&gt;,所以我想要存储在二维数组中的所有字典键和值。这里 List 可以有多个 Tuples 并且每个 TupleDictionary 所以我想获取所有 dict 键和值
【解决方案2】:

在您的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;
}

(我稍微更改了代码以处理Dictionary&lt;string, string&gt; 类型的_recordMasterList。此外,在这样的代码中,我发现显式声明我的变量类型比依赖implicit typing 更清晰。那说,对于数组,我更喜欢使用implicit array typing - new [] 而不是new string[]。)

【讨论】:

  • 是的,但它返回 string([[key][value]],[])... 我想作为 string([keys],[values])
【解决方案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;
}

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      您的问题仍然有点令人困惑。这是您正在寻找的行为吗?

      (我知道这个答案可以优化很多,但它是找出你想要的最简单的方法。)

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

      【讨论】:

        猜你喜欢
        • 2020-11-18
        • 1970-01-01
        • 2020-04-02
        • 1970-01-01
        • 2010-11-09
        • 2019-11-17
        • 2017-05-06
        • 1970-01-01
        • 2015-09-23
        相关资源
        最近更新 更多