Generic Database Linq
Given a function as below, i can take a single table from my database and write a lambda using the Where extension method and pretty much build all the other cases using a simple wrapper method and supplying a filter.
public void getPeople(Expression<Func<tblPeople, bool>> filter, Action<List<tblPeople>> callba开发者_JS百科ck)
{
var query = from People in sdContext.tblPeople.Where(filter)
select People;
var DSQuery = (DataServiceQuery<tblPeople>)query;
DSQuery.BeginExecute(result =>
{
callback(DSQuery.EndExecute(result).ToList<tblPeople>());
}, null);
}
What I really would like to do now is write an even more generic method, that abstracts out the tblPeople to a parameter. This way I could just have one line methods for all my calls, at least those that provide lists! How can I take this and build:
public void getTable<T>(Expression<Func<T, bool>> filter, Action<List<T>> callback)
{
var query = from DB in sdContext.T.Where(filter)
select DB;
var DSQuery = (DataServiceQuery<T>)query;
DSQuery.BeginExecute(result =>
{
callback(DSQuery.EndExecute(result).ToList<T>());
}, null);
}
Is this possible!
This might work...
public void getTable<T>(Expression<Func<T, bool>> filter, Action<List<T>> callback)
{
var query = from DB in sdContext.GetTable<T>.Where(filter)
select DB;
var DSQuery = (DataServiceQuery<T>)query;
DSQuery.BeginExecute(result =>
{
callback(DSQuery.EndExecute(result).ToList<T>());
}, null);
}
精彩评论