LINQ 字典到锯齿状数组?

发布于 2024-11-07 15:17:18 字数 720 浏览 0 评论 0原文

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

There is a method that returns 2D array, this method querying a dictionary from LINQ query and trying to store keys and values in 2D array.

But I am not able to do that

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

But its failed, is there any way?

EDIT:
Type of _recordMasterList

List<Tuple<string, Dictionary<string, string>>> _recordMasterList;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

葬花如无物 2024-11-14 15:17:18

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

Create a string array instead of an object in the query, then the ToArray will return an array of arrays:

public string[][] GetRecordFields(string selectedRecord) {
  return (
    from record in _recordMasterList
    where record.Item1 == selectedRecord
    select new string[] {
      record.Item2.Keys,
      record.Item2.Values
    }
  ).ToArray();
}
停顿的约定 2024-11-14 15:17:18

在您的 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 类型的 _recordMasterList。此外,在这样的代码中,我发现声明变量更加清晰显式键入,而不是依赖隐式键入。我更喜欢使用隐式数组类型 - new [] 而不是 new string[]。)

In your select you need to create a string array (new []). In your example you were creating a 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;
}

(I've changed the code slightly to deal with a _recordMasterList of type Dictionary<string, string>. Also, in code like this I find it clearer to declare my variable type explicitly, rather than relying on implicit typing. That said, with arrays I prefer to use implicit array typing - new [] rather than new string[].)

眸中客 2024-11-14 15:17:18

这不是单行 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;
}

Not a one liner LINQ magic but here it is:

/// <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;
}
假装爱人 2024-11-14 15:17:18

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

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

A more generic version with reversed dimensions:

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;
}
无法言说的痛 2024-11-14 15:17:18

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

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

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

Your question is still a little bit confussing. Is this the behaviour you are looking for?

(I know that this answer could be optimised a lot, but it seams the easiest way to figue out what you want.)

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文