2

This is situation:

if count items is either 0,1,5,7,8,9,10 then string = "string one"
if count items is either 2,3,4 then string = "string two"

I tried with (inside cs razor view)

@if (@item.TotalImages == 1 || 5 || 7 || 8 || 9 || 10)
{
   string mystring = "string one"
}

but I'm getting this error

operator || cannot be applied to operands of type bool or int

7 Answers 7

7

Or perhaps

var accepted = new HashSet<int>(new[] {1, 5, 7, 8, 9, 10});

@if (accepted.Contains(item.TotalImages))
{
   string mystring = "string one"
}
Sign up to request clarification or add additional context in comments.

Comments

7

You have wrong syntax for or operator.

Change to.

@if (@item.TotalImages == 1 || @item.TotalImages == 5)
{
   string mystring = "string one"
}

Comments

4

The In extension method could be a syntactic sugar for a situation like this:

public static class CLRExtensions
{
    public static bool In<T>(this T source, params T[] list)
    {
        return list.Contains(source);
    }
}

So basically instead of using multiple or operator, you could simply write:

@if (@item.TotalImages.In(1, 5, 7, 8, 9, 10)
{
}

1 Comment

nice usage of generics for this application.
3

Take a closer look at the error message:

operator || cannot be applied to operands of type bool or int

And your code:

@if (@item.TotalImages == 1 || 5)

You're applying the || operator to a bool (@item.TotalImages == 1) and an int (5). 'True or 5' doesn't make sense. Neither does 'False or 5'

Basically, all you need to do is make both sides of the || operator booleans.

@if (@item.TotalImages == 1 || @item.TotalImages == 5)

There are (of course) lots of other clever ways to do this, but that's probably the most straight forward.

Comments

1

You may end up with a very big 'if' statement if you want to check all those possibilities. A terser way to do it using LINQ would be:

@if ((new List<int>{ 0, 1, 5, 7, 8, 9, 10 }).Contains(@item.TotalImages))
{
    string mystring = "string one"
}

This way you can more easily see and maintain the list of numbers to check against (or, indeed pass them in from somewhere else).

1 Comment

The "Contains" method of HashSet<T> is more efficient than that of List<T>.
0

Between "||" always must be an expression, that can be transleted to Boolean (true / false):

@if (@item.TotalImages == 1 || @item.TotalImages == 5 || @item.TotalImages == 7 || @item.TotalImages == 8 || @item.TotalImages == 9 || @item.TotalImages == 10)
    {
       string mystring = "string one"
    }
@else @if(@item.TotalImages == 2 || @item.TotalImages == 3 || @item.TotalImages == 4)
    {
       string mystirng = "string two"
    }

Comments

0

I'd use a switch:

@switch (@item.TotalImages)
{
    case 0:
    case 1:
    case 5:
    case 7:
    case 8:
    case 9:
    case 10:
        s = "string one";
        break;
    case 2:
    case 3:
    case 4:
        s = "string two";
        break;
    default:
        throw new Exception("Unexpected image count");
}

Oddly, nobody has suggested a dictionary:

private string stringOne = "string one";
private string stringTwo = "string two";

private Dictionary<int, string> _map = new Dictionary<int, string>
{
    { 0, stringOne },
    { 1, stringOne },
    { 2, stringTwo },
    { 3, stringTwo },
    { 4, stringTwo },
    { 5, stringOne },
    { 7, stringOne },
    { 8, stringOne },
    { 9, stringOne },
    { 10, stringOne },
}

then

@var s = _map[@item.TotalImages];

This approach makes it easier to see, for example, that you have not handled the case where TotalImages == 6.

2 Comments

Why would you favour a switch statement as opposed to the other solutions proposed? Just interested...
@Holf on the assumption that the compiler can do it more efficiently than I can. Also, some other answers mentioned maintainability. If the logic is subject to change, then I'd be more inclined to use a Dictionary<int, string> as it would be more flexible.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.