1

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.

7
  • 2
    Well first, for any given x, x.Key will never equal "ProductName", "ShortDescription", and "Description" all at the same time. What type do you expect for x? Is it a dictionary or associative array such that the expression would be something like x => x["ProductName"]=="product1" && x["ShortDescription"].Contains("short desc") || x["Description"].Contains("desc")?
    – T N
    Commented Feb 4, 2022 at 21:10
  • Welcome to StackOverflow. In order to get a better answer, please provide more details in your question. What is the starting generic type for your IQueryable<>? Is it ProductTranslate? What is the desired output type? Func<ProductTranslate, bool>? Expression<Func<ProductTranslate, bool>>? IQueryable<ProductTranslate>? IQueryable<Dictionary<string, string>>? Func<KeyValuePair<string, string>, bool>? Commented Feb 4, 2022 at 21:12
  • Side note: Consider whether you need to group the ShortDescription and Description conditions, so a Description match still requires a Productname match. What you have now is: (product-matches && Short-description-matches) || (description-matches).
    – T N
    Commented Feb 4, 2022 at 21:15
  • i have a LanguageTranslate Table in database. Database scheme is Key-Value. Exc. L LanguageId =1 Key=ProductName Value=Water ProductTranslate class is not entity. it is translate content class. But i get only this class properties from database. I don't want to pull unnecessary records from the database where I don't need to use them. I will only pull the key values ​​of the property in this class. there is no problem in this part. however, I wanted to set up such a structure because the query is very complex where I want to bring the contents of the language I want.
    – brk.gs
    Commented Feb 4, 2022 at 21:35
  • Strange sample. Do you need to generated dynamically such filter? Commented Feb 5, 2022 at 7:33

2 Answers 2

0

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
0

My rough 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.

enter image description here

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Feb 5, 2022 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.