LINQ dictionary to jagged array?
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
开发者_Go百科 }).ToArray();
return recordFields;
}
But its failed, is there any way?
EDIT:
Type of _recordMasterList
List<Tuple<string, Dictionary<string, string>>> _recordMasterList;
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();
}
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[]
.)
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;
}
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;
}
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;
}
精彩评论