79361571

Date: 2025-01-16 12:30:28
Score: 2
Natty:
Report link

I want to share my simple solution.

I had similiar problem. Thanks for all suggestions. But I like simple solutions.

What is my problem? I have a list like category systems. Some categories are upper categories and some of them are lower categories. I need list them all on the same time in the correct order.

Something like this.

1. Category 1
2. Category 2
2.1 Sub Category 1
2.2 Sub Category 2
3. Category 3

I have a nullable int field UpperCategoryId and non-nullable int Order field and text field.

When I list categories, I calculate a double number like this

DoubleOrder = Convert.ToDouble(x.UpperCategoryId.HasValue ? x.UpperCategory.Order + (x.Order/10.0) : 0 + x.Order)

This takes UpperCategories orders same. Takes subcategories values as decimal. Then I order by DoubleOrder

.OrderBy(x => x.DoubleOrder)

And my result is exactly what I want.

results

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Blacklisted phrase (0.5): I need
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (0.5):
Posted by: Hakkı