I've added a query to my repository, that queries for a list of orders that match the email search key. But when I step through the method, my result is null although the email string matches the email stored in my orders database document, "email": "brianvarley@gmail.com"
I'm using the MongoDB.Net driver to communicate with the MongoLabs repository. My other queries to query all documents works so doesn't seem to be a connection issue.
I tried hovering over c.Email
, to check the database result, but no values pop up.
Does anyone know how I can debug the null result further?
This is the method that queries orders by email:
public async Task<List<OrderModel>> GetAllByEmailAsync(string email)
{
if (orderList == null)
await LoadDbAsync();
return orderList.Where(c => c.Email == email).ToList();
}
I added a dump to check orderList contents, but that shows a count = 0:
string dump = string.Join(",", orderList.ToString());
This is the LoadDBAsync()
, when I step through the code, this method is skipped as OrderList isn't null:
public async Task LoadDbAsync()
{
var orderCollection = StartConnection();
try
{
orderList = await orderCollection.Find(new BsonDocument()).ToListAsync();
}
catch (MongoException ex)
{
//Log exception here:
MessageBox.Show("A connection error occurred: " + ex.Message, "Connection Exception", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
And the StartConnection()
called from LoadDbAsync:
public IMongoCollection<OrderModel> StartConnection()
{
var client = new MongoClient(connectionString);
var database = client.GetDatabase("orders");
//Get a handle on the customers collection:
var collection = database.GetCollection<OrderModel>("customer_orders");
return collection;
}
If your list isn't null
but is empty, it will never return anything... Change your code for:
public async Task<List<OrderModel>> GetAllByEmailAsync(string email)
{
if (orderList == null || orderList.Count() == 0)
await LoadDbAsync();
return orderList.Where(c => c.Email == email).ToList();
}