tags:

views:

252

answers:

6

Hi,

I have a Linq Query where I do the following:

query = context.Select(a => new 
{
   Course = (CourseType)a.CourseCode,
   CourseDetail = sting.Format("Course: {0}\r\nCourse Detail: {1}", ((CourseType)a.CourseCode).ToString(), a.CourseDetail)
});

enum CourseType{
 Unknown = 0,
 FullTime = 1,
 PartTime = 2
}

a.CourseCode is an int and a.CourseDetail is a string. I now bind a label inside a grid to this query. I set the Text to <%# Eval("Course")%> and the Tooltip to <%# Eval("CourseDetail")%>. Although the text in the label correctly displays the value expected from Enum.ToString(), the Tootip always shows the value of the integer value of the enum as 1,2,3...

Whats causing this?

Kind regards,

+1  A: 

Are you sure that's the exact code you're using?

There's a typo: sting.Format instead of st**r**ing.Format, so I guess you've retyped the code for this question. Check to make sure all your brackets are in the correct place etc.

I've tried the following code, prints out "Fulltime", so the .ToString method should work as you're expecting:

class Program
{
    enum CourseType
    {
        Unknown = 0,
        Fulltime = 1,
        Parttime = 2
    }

    static void Main(string[] args)
    {
        var i = 1;
        Console.WriteLine("Coursetype: {0}", ((CourseType)i).ToString());
    }
}
roomaroo
A: 

Not directly an answer, but just a question, why are you typing:

((CourseType)i).ToString()
((CourseType)a.CourseCode).ToString()

The string.Format takes objects as ParamArray and calls .ToString() on each of them. The additional .ToString() you use performs just the creation of a new string that is not needed.

BeowulfOF
Yeah - it's unnecessary but I included it in my sample just to make it explicit.
roomaroo
A: 

are you actually setting the right value in there? I only ask because your enum CourseTypes only goes up to 2, yet you state:

the Tootip always shows the value of the integer value of the enum as 1,2,3...

And in your code you have:

   Course = (CourseType)a.CourseCode,
   CourseDetail = sting.Format("Course: {0}\r\nCourse Detail: {1}", ((CourseType)a.CourseCode).ToString(), a.CourseDetail)

It looks like you're trying to parse the course code as a course type (which could just indicate a poor choice of parameter name), but might be where your problem lies.

If I have:

   var Course = (CourseType)3;

That will compile just fine, even though the enum only goes to 2, but when I call .ToString(), I will just get "3" back.

Zhaph - Ben Duguid
A: 

I just encountered what I think is the same problem while working with LINQ. The following code always returned the numerical representation of the OfferType enum.

public enum OfferType
{
  Unknown,
  Dining,
  Shopping,
  etc...
}

return (from so in db.Offers
        where so.Status == status
        select new
               {
                 so.Headline,
                 so.BusinessName,
                 OfferType = ((OfferType) so.OfferTypeId).ToString(),
                 ExpirationDate = so.ExpirationDate.ToShortDateString(),
                 StartDate = so.StartDate.ToShortDateString(),
               }).ToList();

In the line

OfferType = ((OfferType) so.OfferTypeId).ToString(),

OfferType would always be the numerical representation of the enum value, not the expected string representation. The value of so.OfferTypeId is correct. Changing the line to

OfferType = ((OfferType) 1).ToString(),

would return the expected string representation of the enum.

I had to move the cast and ToString to another method to get it to work.

OfferType = GetOfferType(so.OfferTypeId),

...

public static string GetOfferType(int id)
{
  return ((OfferType) id).ToString();
}
+1  A: 

I think this is a bug (or feature) LINQ to SQL. I would guess that it rewrites

query = context.Select(a => new 
{
   Course = (CourseType)a.CourseCode,
   CourseDetail = sting.Format("Course: {0}\r\nCourse Detail: {1}",
                        ((CourseType)a.CourseCode).ToString(), a.CourseDetail)
});

to

query = context.Select(a => new 
{
   Course = (CourseType)a.CourseCode,
   CourseDetail = sting.Format("Course: {0}\r\nCourse Detail: {1}",
                               a.CourseCode.ToString(), a.CourseDetail)
});

Because the enum is an int in the database, so this is equivalent, right? (Wrong.)

One way to fix this, would be to put the ToString() in the hands of the Framework, not LINQ to SQL, by using

query = context.Select(a => new 
{
   Course = (CourseType)a.CourseCode,
   CourseDetail = a.CourseDetail
}.ToArray() // get LINQ to SQL out of the way
.Select(a => new
{
   Course = a.Course,
   CourseDetail = sting.Format("Course: {0}\r\nCourse Detail: {1}",
                               a.Course, a.CourseDetail)
});
Ruben
A: 

The static Enum.GetName() method is used to get the name of the constant for a given enum value. Try calling Enum.GetName(typeof(CourseType), a.CourseCode) on your course detail line, like this:

CourseDetail = String.Format("Course: {0}\r\nCourse Detail: {1}", Enum.GetName(typeof(CourseType), a.CourseCode), a.CourseDetail)
itsmecurtis