通用数据库 Linq
给定如下函数,我可以从数据库中获取一个表,并使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能有效...
This might work...