tags:

views:

393

answers:

6

In sql, I can make an if statement like the following If MY_STATE in (1,2,3,4)

In C# I have to type if(MY_STATE == STATE.CT || MY_STATE == STATE.MA || MY_STATE == STATE.VA || MY_STATE == STATE.RI)

This is obviously more clunky. Can I use LINQ to mimic the "in" statement of sql?

I'm looking for something like if(MY_STATE in (STATE.CT, STATE.MA, STATE.VA, STATE.RI))

+5  A: 

You want to use Contains, which maps onto the SQL IN. I'm assuming State is an enum and stored as an integer.

var states = new int[] { (int)State.CT, (int)State.MA, (int)State.VA, (int)State.RI };

var query = db.States.Where( s => states.Contains( s.State ) );
tvanfosson
You were faster ;-) I don't like the int cast though, but that's probably just me.
Benjamin Podszun
I'm not sure that Contains on an `IEnumerable<State>`, when the field on the DB is an int will work without the cast and I don't think you could cast the DB field to an instance of the enum. I could be wrong, though. I didn't check it. If so, then I agree that making it an array of the enum type and dropping the cast would be the way to go.
tvanfosson
+2  A: 

In c++ I would use the switch statement.

switch (MY_STATE) {
  case STATE.CT:
  case.STATE.MA:
  case.STATE.VA:
  case.STATE.RI:
    ....
    break;
}

just jotted it down from memory. So you might correct some syntax issues.

Peter Schuetze
This is one of the few things that really ticks me off about C#. You can do this in C++ but you can't do this in C#. I have no idea why.
diadem
@diadem: what do you mean? C# does have a switch statement.
nikie
You can in c# if my_state is an enum or an int.
Manitra Andriamitondra
You mean, that you can't use 'switch'?
Piotr Justyna
@diadem: this will work in C# as well. When there is no code in the case statements, you can stack them to allow fall-through just like the example.
Scott Anderson
@manitra: You can even use switch if my_state is a string
nikie
I'm pretty sure Scott got it right: diadem was probably confused about needing a break; or goto someOtherCase; - which isn't needed in this sample.
Benjamin Podszun
You are right. I got confused for a second - the limitation is that you need a break before you have another set of case statements, unlike C++.
diadem
+13  A: 
if (new [] {State.CT, State.MA, State.VA, State.RI}.Contains(myState)) {
  // There you go
}
Benjamin Podszun
I <3 c# in .Net 3.5!
Terry Donaghe
It looks nice. I like easy to read code that is even easy to maintain. Does somebody knows how the performance is in comparison to the switch statement?
Peter Schuetze
Worse. Switch and if statements are basically comparisons and jumps. This is creating an array and doing a loop, compare, break.Does it matter though? Depends on your usecase. Measure it :)
Benjamin Podszun
+1  A: 

You could do something like:

    enum MyEnum
    {
        A, B, C, D
    }

    // ...
    MyEnum e = MyEnum.A;
    if (new []{ MyEnum.A, MyEnum.B }.Contains(e))
       Console.WriteLine("Yeah!");
bruno conde
+7  A: 

You could use an extension method:

public static class ExtensionMethods
{
    public static bool EqualsAny<T>(this T comparer, params T[] values)
    {
        foreach (T t in values)
            if (comparer.Equals(t))
                return true;

        return false;
    }
}

and use it like:

if (myState.EqualsAny(State.CT, State.MA, State.VA, State.RI)) 
{
    // ...
}
Callum Rogers
+1 very elegant.
Scott Anderson
That's actually not necessary: You already have .Any() on IEnumerable<T>. So if(new [] {"CT", "MA", "VA", "RI"}.Any(e => e == value)) {} would do the same.
Benjamin Podszun
But you have to type out the new[] { ... } and the lambda expression each time.
Callum Rogers
Good point. Technically it should be the same (speed). You could use the .Any in your EqualsAny() though. ;-) I digress. It works just as well and is a valid solution of course.
Benjamin Podszun
A: 

You could write your own extension method to do that:

static bool IsOneOf<T>(this T value, params T[] set)
{
    return set.Contains(value);
}

Usage: MY_STATE.IsOneOf(STATE_A, STATE_B, STATE_C)

(It's slower at runtime, though. It has to create a temporary array and all parameters to IsOneOf have to be evaluated)

nikie