LINQ dictionary for gear array?

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

But I can't do it

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 it failed, is there any way?

EDIT: type _recordMasterList

 List<Tuple<string, Dictionary<string, string>>> _recordMasterList; 
+4
source share
5 answers

Create a string array instead of the object in the request, then 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(); } 
+3
source

In your select you need to create an array of strings ( new [] ). In your example, you created 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 changed the code a bit to deal with _recordMasterList 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 . However, less, with arrays, I prefer to use an implicit input array - new [] , rather than new string[] .)

+3
source

Not one LINQ magic liner, 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; } 
+3
source

Your question is still a little confusing. Is this the behavior you are looking for?

(I know that this answer could be optimized a lot, but it provides the easiest way to figure 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; } 
0
source

More general resized version:

 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
source

All Articles