如何从客户端过滤行并选择所需的列
考虑这种情况:
我有一个参考数据,我编写服务将其返回给我的客户。在我的程序中,我以不同类型使用此数据。
我想以动态方式从客户端的数据库中准确获取我想要的内容。
我想使用这样的代码:
public List<TResult> FindAll<T, TResult>(Func<T, bool> exp, Func<T, TResult> selector, int PageSize) where TResult : class
{
}
问题是我无法将我的服务接口声明为通用,并且我无法以这种方式使用该代码:
public List<TResult> FindAll<Order, TResult>(Func<Order, bool> exp, Func<Order, TResult> selector, int PageSize) where TResult : class
{
using (DataClasses1DataContext dc = new DataClasses1DataContext())
{
return dc.Orders.Where(exp).Select<Order, TResult>(selector).ToList<TResult>();
}
}
因为:
函数中的订单充当参数而不是订单类。
我的 TResult 未在服务中声明
我该如何执行此操作?多谢。
Consider this scenario:
I have a reference data that I write service to return this to my clients.In my program I use this data in different types.
I want to get exactly what I want from database from client and in dynamic manner.
I want to use such this code:
public List<TResult> FindAll<T, TResult>(Func<T, bool> exp, Func<T, TResult> selector, int PageSize) where TResult : class
{
}
the problem is I can't declare my service interface as generic and I can't use that code this way:
public List<TResult> FindAll<Order, TResult>(Func<Order, bool> exp, Func<Order, TResult> selector, int PageSize) where TResult : class
{
using (DataClasses1DataContext dc = new DataClasses1DataContext())
{
return dc.Orders.Where(exp).Select<Order, TResult>(selector).ToList<TResult>();
}
}
Because:
Order in function act as parameter not Order class.
my TResult is not declared in service
How I can do this? Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使您的方法变得通用,您无法将委托作为参数传递,并且您可能也无法将表达式树作为参数传递,因为我认为它默认情况下不可序列化。您应该使用一些已经为您执行此操作的 API - 检查 WCF 数据服务 或表达式树序列化。
You cannot make your method generic, you cannot pass delegate as parameter and you probably cannot pass expression tree as parameter as well because I think it is by default not serializable. You should use some API which already do this for you - check WCF Data Services or Expression Tree Serialization.