I am stuck in a problem where I need to convert a list into Dictionary > BsonDocument to import in MongoDB.
The list has the name of column and the values to insert in that column. But I am getting key already exits exception as soon the compiler enter in loop. Any suggestion?
void Main()
{
List<ListRow> myList = new List<ListRow>();
myList.Add(new ListRow { columnName = "column1", results = new List<string> { "a1", "b1", "c1" } });
myList.Add(new ListRow { columnName = "column2", results = new List<string> { "a2", "b2", "c2" } });
myList.Add(new ListRow { columnName = "column3", results = new List<string> { "a3", "b3", "c3" } });
List<BsonDocument> batch = new List<BsonDocument>();
foreach (var row in myList)
{
var dictionary = row.results.ToDictionary(x => row.columnName, x => x);
batch.Add(dictionary);
}
// Print batch
// Add to MongoDB
}
public class ListRow
{
public string columnName { get; set; }
public List<string> results { get; set; }
}
Expected result to pull