views:

111

answers:

4

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).

A: 

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

Benjamin Podszun
And how do you intend to serialize the delegate to send it to the WCF service ?
Thomas Levesque
cheers ;) I still dont understand what the method signature of GetGroups would be though.
John Nicholas
Thanks, Thomas - my bad.
Benjamin Podszun
+3  A: 

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.

Thomas Levesque
winnah thanks both. Have written something similar for a game previously ....
John Nicholas
A: 

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

Pablo Castilla
A: 

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.

Eric Lippert
lol linq-To-Tortoise ... sounds like a dangerous beast.I tend to shy away from custom scripting languages - you are giving up a lot of control and its just begging to cause huge issues in the future. But I will be looking into IQueryable a lot more ... thanks.
John Nicholas