views:

55

answers:

1

I am working on a project that uses Albahari's PredicateBuilder library http://www.albahari.com/nutshell/ to create a linq expression dynamically at run time. I would like to find a way to translate this dynamically created linq predicate of type Expression<Func<T, bool>> into a readable english statement at runtime.

I'll give a statically created linq statement as an example:

from p in Purchases
select p
where p.Price > 100 && p.Description != "Bike".

For this linq statement I would want to dynamically generate at runtime an english description along the lines of:

"You are searching for purchases where the price is greater than 100 and the description is not bike".

Are there any libraries that already exist which accomplish this goal, keep in mind I am using PredicateBuilder to dynamically generate the where predicate. If no solution exists how would you go about building a solution?

Thanks!

A: 

This caught my attention so I downloaded ExpressionSerializationTypeResolver.cs and ExpressionSerializer.cs and then I:

class Purchase
{
    public decimal Price {get;set;}
    public string Description {get;set;}
}

...

var purchases = new List<Purchase>() { new Purchase() { Price = 150, Description = "Flute" }, new Purchase() { Price = 4711, Description = "Bike" } };

Expression<Func<IEnumerable<Purchase>>> queryExp = () => from p in purchases
    where p.Price > 100 && p.Description != "Bike"
    select p;

ExpressionSerializer serializer = new ExpressionSerializer();
XElement queryXml = serializer.Serialize(queryExp);

and then I got into problems, but maybe you could do something with the pretty big expression tree of your query? You can find it here.

Jonas Elfström