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://learn.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 |
x => x["ProductName"]=="product1" && x["ShortDescription"].Contains("short desc") || x["Description"].Contains("desc")
?IQueryable<>
? Is itProductTranslate
? What is the desired output type?Func<ProductTranslate, bool>
?Expression<Func<ProductTranslate, bool>>
?IQueryable<ProductTranslate>
?IQueryable<Dictionary<string, string>>
?Func<KeyValuePair<string, string>, bool>
?(product-matches && Short-description-matches) || (description-matches)
.