English 中文(简体)
LINQ 字典到gged阵阵列?
原标题:LINQ dictionary to jagged array?

有一种方法使2D阵列回归,这种方法将1名词典从LINQ 查询,并试图在2D阵列储存钥匙和价值。

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

但是,它失败了吗?

EDIT: Type of _recordMasterList

List<Tuple<string, Dictionary<string, string>>> _recordMasterList;
最佳回答

• 建立阵列,而不是在座标中标的,然后由<代码>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();
}
问题回答

不是一线分子,而是:

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

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

你的问题仍然令人困惑。 你们的这种行为是否在寻求?

(我知道,这一答案可以选择很多,但最容易地回避你想要的东西。)

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签