views:

93

answers:

5

I'm pulling some data from a table using LINQ 2 SQL...one of the pieces of data is a value that represents an enumation in my application code.

What is the simplest way to make a comparison between the data returned in LINQ objects and the enumeration in the application code. So for example

enum SomeEnum
{
  First
  Second
}

then in the method I have

Table<LinqObject> objects = dc.GetTable<LinqObject>();

foreach (var item in objects)
{
   // What's the simplest way to do this comparison???
   if (item.SomeNullableInteger == SomeEnum.First) // Note I realise this doesn't work!!!
   {
      // Do something...
   }
}

I could do this

SomeEnum.First.Equals(item.SomeNullableInteger)

or I could store the enumeration names in the database and then i'd be able to do this

Enum.GetName(SomeEnum, SomeEnum.First) == item.SomeNullableName

is there a better way? The enum only has two items and they're pretty fixed...could maybe have a third or a fourth but will probably never grow beyond that. So having a whole table seems like overkill.

Actually this is a duplicate of http://stackoverflow.com/questions/502905/c-int-to-enum-conversion

+3  A: 

This should work as expected - just cast to the base type.

item.SomeNullableInteger == (Int32)SomeEnum.First

UPDATE

The best and cleanest solution is probably to update your DBML file.

  1. Open the DBML file in the designer.
  2. Select the enumeration type property of the entity.
  3. Open the properties window.
  4. Change the field Type of the selected property from System.Int32 to something like global::SomeNamespace.SomeEnum. Maybe it will work without the global qualifier, but I am not sure.

Now, if the code is regenerated, the property will be of the enumeration type instead of a integer. If you have nullable properties, you must of course use a nullable enumeration type.

Daniel Brückner
That's exactly what I was looking for...this also allows me to use the item.SomeNullableInteger (which is of course no longer an integer but the DB type is an integer) in switch statements. I did look at the the DBML definitions and though about changing the type...but because the enum wasn't in the drop down I didn't change it :-), I'm clearly a library user not a library writer.
Michael Prewecki
oh and yes it does work without the global
Michael Prewecki
Thanks for the info.
Daniel Brückner
+1  A: 

You could cast the Enum to int:

if (item.SomeNullableInteger == (int)SomeEnum.First)
{
     // Do something...
}
bruno conde
Checking HasValue is not needed, because the lifted comparisons operator handles this correctly.
Daniel Brückner
Sure. Corrected the answer.
bruno conde
+2  A: 

Casting the enum to an int should do the trick.

Michael McCloskey
+2  A: 

According to the official documentation, you could cast your enum as an int to compare.

if (item.SomeNullableInteger == (int)SomeEnum.First)

http://msdn.microsoft.com/en-us/library/sbbt4032.aspx

For code clarity, it might be helpful to declare your enum as

enum SomeEnum
{
   First = 1;
   Second = 2;
}
David Stratton
they are actually...just forgot it in the code I wrote for the question :-)
Michael Prewecki
+1  A: 

to have

item.SomeNullableInteger == SomeEnum.First

to work:

  • be aware that First = 0 & Second = 1 (following the declaration of that enum)
  • SomeNullableInteger is of type int
  • And of course, SomeNullableInteger = 0 means First for you & SomeNullableInteger = 1 means second.
najmeddine
Good point, unless you specifically assign the value of the enum. + 1 for thinking to point that out, though.
David Stratton