Home > Mobile >  How can I convert lambda expression with C#?
How can I convert lambda expression with C#?

Time:02-05

I need IQueryable extension method for my custom query. Example method and I want be result is below. How can I do this ?

Input =>

.Where(x => x.Id == 1)
.FilterContent<ProductTranslate>(x => x.ProductName == "product1" 
                                      && x.ShortDescription.Contains("short desc") 
                                      || x.Description.Contains("desc"), 1)

Output=>

x => (x.Key == "ProductName" && x.Value=="product1")
     && (x.Key == "ShortDescription" && x.Value.Contains("short desc") 
         || (x.Key == "Description" && x.Value.Contains("desc")))

Regards.

CodePudding user response:

My roughly database scheme is below. I have a TranslateRecords and TranslateResource tables for covers all entities multilanguage content.i put the translateId column with multiple language options entity table. so I can use all my content in one table without opening a separate translate table for each entity.

I just want to make an extension method because very complex queries occur in the filters I mentioned in the topic.

I will take your suggestions into consideration. thank you.

enter image description here

CodePudding user response:

I think I understand what you are asking, but I will say the solution is way beyond me.

It sounds like to me that you need a function that parses an expression tree of type Func<Product, bool> (or more generally Func<T, TResult>) and convert it into an expression tree of type Func<ProductTranslate, bool> (or more generally Func<T2, TResult>).

The article https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/expression-trees/how-to-modify-expression-trees may be a start. The translation function would walk the original tree and build up a new modified tree. Luckily, it looks like ExpressionVisitor base class can do most of the work. You would need check each node to see if it is a T.Property reference and replace it with the appropriate T2.Property, T2["Property"], T2.GetPropertyValueByName("Property") or similar. You may also have to identify and replace whatever node accepts this T and an input parameter and replace it with a this T2 equivalent. Tracking the Product ID and language ID may involve additional changes.

As to how to detect and interpret the T.Property node, that may take some digging on your part, but I expect there must be some tools out there to dump expression trees in human readable forms that could help. Perhaps you can dump an original expression tree and a hand-coded objective expression tree to examine the differences.

If you get through all that, applying the new function to the IQueryable<>.Where() function should be the easy part.

P.S., Out of interest, is the following roughly the database structure you are working with, where your ProductTranslateKey class is your in-memory on-demand loaded representation of LanguageTranslate?

Products:

ID ProductName ShortDescription Description
1 Water Water is good Water is very good
2 Fire Fire is bad Fire is very bad

Languages:

ID Code Description
1 EN English
2 DE Deutsch
3 FR français

LanguageTranslate:

ProductID LanguageId Key Value
1 1 ProductName Water
1 1 ShortDescription Water is good
1 1 Description Water is very good
1 2 ProductName Wasser
1 2 ShortDescription Wasser ist gut
1 2 Description Wasser ist sehr gut
1 3 ProductName Leau
1 3 ShortDescription Leau est bonne
1 3 Description Leau est très bonne
2 1 ProductName Fire
2 1 ShortDescription Fire is bad
2 1 Description Fire is very bad
2 2 ProductName Feuer
2 2 ShortDescription Feuer ist schlecht
2 2 Description Feuer ist sehr schlecht
2 3 ProductName Feu
2 3 ShortDescription Le feu est mauvais
2 3 Description Le feu est très mauvais
  •  Tags:  
  • Related