我希望能够使用 lambda 表达式来指定要通过 wcf 服务返回的值范围

发布于 2024-08-24 04:06:05 字数 455 浏览 7 评论 0 原文

我不知道这是否可能......但这会很酷。问题是这是否可能,但如果可能的话,举一个例子。

我不确定您将使用什么方法签名来将 lambda 表达式传递到其中。

例如,方法IList GetGroups()

您将如何修改它以便能够将 lambda 表达式传递给它?

下一个问题是如何编写 lambda 表达式以返回所有 Group 对象,例如

  • where .deleted == false
  • where .DateAdded > aDate

是的,我想要棒上的月亮;) 提前致谢。

(编辑我认为这实际上有点考虑不周,因为数据访问层实际上会获取数据......但假设您正在通过服务查询某些对象集合,并且不必担心 dal )。

I have no idea if this is possible ... but it would be cool. the question is whether it is possible but then a bit of an example if possible.

I am not sure what method signature you would use to pass the lambda expression into.

Eg the method IList<Group> GetGroups()

How would you modify that to be able to pass a lambda expression into it?

The next question is how would you code a lambda expression to return all Group objects where for example

  • where .deleted == false or
  • where .DateAdded > aDate

Yeah, I want the moon on a stick ;)
Thanks in advance.

(edit I am thinking this is a bit ill-conceived actually because of the data access layer that would actually fetch the data ... but assume that you are querying some object collection over the service and don't have to worry about the dal).

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

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

发布评论

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

评论(4

各空 2024-08-31 04:06:06

您可以

  • 定义一种简单的查询语言,您的后端服务可以理解
  • 该语言,Web 服务公开一个方法,该方法采用该查询语言中的字符串,
  • 编写一个客户端转换系统,该系统接受充满表达式树的 IQueryable,并将其转换为查询语言
  • 现在,客户端可以直接用您的查询语言编写查询,也可以编写 LINQ 查询,您的翻译器将其转换为您的查询语言
  • 嘿,​​您刚刚发明了 LINQ-To-Tortoise!

Matt Warren 有十七篇博客文章介绍如何执行此类操作。

You could

  • define a simple query language that your back-end service understands
  • the web service exposes a method that takes a string in this query language
  • write a client-side conversion system that takes in an IQueryable full of expression trees, and translates that into the query language
  • now the client can either write queries in your query language directly, or can write LINQ queries which your translator turns into your query language
  • hey, you just invent LINQ-To-Tortoise!

Matt Warren has seventeen blog articles about how to do this sort of thing.

一页 2024-08-31 04:06:06

我认为 RIA 服务可以满足您的要求,但我不知道其背后的魔力。

I think that RIA services do what you want, but I do not know the magic behind it.

一场信仰旅途 2024-08-31 04:06:06

您可以传递一个“谓词”:Func,它为给定的 Group 返回 true 或 false。由于您可以在需要 Func 的位置传递 lambda,因此可能类似于:

var fooGroups = GetGroups(g => g.Name.StartsWith("Foo"));< /代码>

You could pass a "predicate": A Func<Group, bool>, which returns true or false for a given Group. Since you can pass a lambda where a Func is expected, this could be something like:

var fooGroups = GetGroups(g => g.Name.StartsWith("Foo"));

星光不落少年眉 2024-08-31 04:06:05

您可以使用 Expression> 类型的参数声明 GetGroups 方法,该参数表示该组必须匹配才能返回的谓词:

IList<Group> GetGroups(Expression<Func<Group, bool>> predicateExpression);

问题是,表达式无法序列化,因此您无法将其发送到 WCF 服务...但是,您可能会找到一种方法来使用 表达式树序列化 项目。

You could declare the GetGroups method with a parameter of type Expression<Func<Group, bool>>, which represents a predicate that the group must match in order to be returned :

IList<Group> GetGroups(Expression<Func<Group, bool>> predicateExpression);

The trouble is, expressions can't be serialized, so you couldn't send it to the WCF service... However, you might find a way to do it with the Expression Tree Serialization project.

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