I have a model call address, with that model i have created a few crud operations, in the process of doing this i want a way i could create a query based on any number of properties:
public static IEnumerable<address> GetByParams(Expression<Func<address, bool>> predicate, int? pageNumber, int? pageSize)
{
using (IDbConnection db = DbFactory.OpenDbConnection())
{
if ((pageNumber != null) && (pageSize != null))
{
var data = db.Where<address>(predicate).Skip((int) pageNumber).Take((int) pageSize).ToList();
if (data.Any())
{
data[0].TotalCount = data.Count();
data[0].TotalPages = (int) (data.Count()/pageSize);
}
return data;
}
//this is the code that creates the error
return db.Where<address>(predicate);
}
}
I have then tried testing this code with:
ViewBag.PossibleBilling = new SelectList(address.GetByParams(x=> x.AddressType == 2,null,null),0);
However this results in the strangest error ever: (the error is not from the selectList )
The given key was not present in the dictionary.
I have tried to google this, but the results are more complicated to fish through than the actual error. I guess it has something to do with me not setting up predicate as the right type, or that something has to happen to predicate before i pass it into where.
Reason for the predicate is that i have a TT file that generates all this for me, so i dont know property names and needs to remain anom.
EDIT:
For clarity on future reading: I am using this against servicestack.ormlite: https://github.com/ServiceStack/ServiceStack.OrmLite
I am trying to create a TT file to build some basic crud methods with my model, I have found this piece of code within ormlite:
System.Collections.Generic.List<T> Where<T>(this System.Data.IDbConnection dbConn, object anonType) Member of ServiceStack.OrmLite.OrmLiteReadConnectionExtensions
Where object anonType eould be my own query / predicate
The paging code is wrong, however this is not the course of the error, The problem solved by drax is that I was using a Where, when all i needed was a select with params.
This improvement changes the usage of code to the following procedure:
public static List<T> Select<T>(this IDbConnection dbConn, Expression<Func<T, bool>> predicate)
This procedure accepts Expression and eliminates the error
thanks
Expression<Func<address, bool>>
instead of a simpleFunc<address, bool>
for the parameter type? – Gillies