Eburen
Eburen

Reputation: 23

How can i convert ternary operator If structure?

I am super beginner at the programming and have a quick question.

i have this code and i want convert it to IF structure but i confused little bit. Can anybody help me to convert it?

var selectedCategoryId = string.IsNullOrEmpty(categoryId5)
    ? string.IsNullOrEmpty(categoryId4)
        ? string.IsNullOrEmpty(categoryId3)
            ? string.IsNullOrEmpty(categoryId2) 
                ? categoryId1 
                : categoryId2
            : categoryId3
        : categoryId4
    :categoryId5;

Check it up on google

Upvotes: 1

Views: 107

Answers (1)

fubo
fubo

Reputation: 45947

select the FirstOrDefault() which is not string.IsNullOrEmpty()

string[] input = { categoryId5, categoryId4, categoryId3, categoryId2, categoryId1 };
var result  = input.FirstOrDefault(x => !string.IsNullOrEmpty(x));

Upvotes: 5

Related Questions