I've been reading that Ienumerables don't run straight away. So I'm trying to find the best way to query a list.
Below is my getAll method. Followed by my filter method. Followed by a preferred filter method (Readability).
My question is, will the 3rd method be the same as querying directly? In other words, will it, or won't it Load ALL myObjects from the DB and then filter? Or filter when getting from the DB.
public static IEnumerable<myObject> getAll()
{
using (var db = Database.OpenConnectionString(blahblah))
{
var queryResults = db.Query("SELECT * FROM vu_myObjects");
return queryResults .Select(x => new myObject(x.myObjectName, x.myObjectF));
}
}
public static IEnumerable<myObject> getAllForFilter(String filter)
{
using (var db = Database.OpenConnectionString(blahblah))
{
var queryResults = db.Query("SELECT * FROM vu_myObjects WHERE ObjectF = @0", filter);
return queryResults.Select(x => new myObject(x.myObjectName, x.ObjectF));
}
}
public static IEnumerable<myObject> getAllForFilter(String filter)
{ // Not checked syntax.
return getAll().Where(x => x.ObjectF = filter);
}
Alternatively, if you have a better suggestion, I'm all ears.