开发者

Generic Database Linq

开发者 https://www.devze.com 2022-12-13 21:17 出处:网络
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 su

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消