开发者

How do I programmatically translate a LINQ query to readable English text that correctly describes the linq expression?

开发者 https://www.devze.com 2023-01-03 05:27 出处:网络
I am working on a project that uses Albahari\'s PredicateBuilder libraryhttp://www.albahari.com/nutshell/ to create a linq expression dynamically at run time. I would like to find a way to translate t

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 Predica开发者_StackOverflow社区teBuilder to dynamically generate the where predicate. If no solution exists how would you go about building a solution?

Thanks!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号