通用数据库 Linq

发布于 2024-08-14 08:17:47 字数 1246 浏览 1 评论 0原文

给定如下函数,我可以从数据库中获取一个表,并使用Where扩展方法编写一个lambda,并使用简单的包装方法并提供过滤器来构建所有其他情况。

  public void getPeople(Expression<Func<tblPeople, bool>> filter, Action<List<tblPeople>> callback)
            {
                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);
            }

我现在真正想做的是编写一个更通用的方法,将 tblPeople 抽象为参数。这样我就可以为我的所有调用(至少是那些提供列表的调用)提供一行方法!我怎样才能接受这个并构建:

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

这可能吗!

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>> callback)
            {
                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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛清羽墨安 2024-08-21 08:17:47

这可能有效...

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文