【问题标题】:Enum.ToString() not working in tooltipEnum.ToString() 在工具提示中不起作用
【发布时间】:2008-12-18 06:04:50
【问题描述】:

我有一个 Linq 查询,我在其中执行以下操作:

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 是一个 int,a.CourseDetail 是一个字符串。 我现在将网格内的标签绑定到此查询。我将 Text 设置为 ,将 Tooltip 设置为 。 尽管标签中的文本正确显示了 Enum.ToString() 所期望的值,但 Tootip 始终将枚举的整数值的值显示为 1,2,3...

这是什么原因造成的?

亲切的问候,

【问题讨论】:

  • 什么是“Enum.ToString() 的预期值”?我希望是一个整数。
  • Enum.ToString() 将为您提供“此实例值的字符串表示形式” - 即值的名称(Unknown、FullTime、PartTime)。

标签: c# asp.net linq


【解决方案1】:

您确定这就是您使用的确切代码吗?

有一个错字:sting.Format 而不是 string.Format,所以我猜你已经为这个问题重新输入了代码。检查以确保所有括号都在正确的位置等。

我已经尝试了以下代码,打印出“Fulltime”,因此 .ToString 方法应该可以正常工作:

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());
    }
}

【讨论】:

    【解决方案2】:

    我认为这是一个错误(或功能)LINQ to SQL。我猜它会重写

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

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

    因为枚举是数据库中的一个int,所以这是等价的,对吧? (错误。)

    解决此问题的一种方法是将 ToString() 交给框架,而不是 LINQ to SQL,使用

    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)
    });
    

    【讨论】:

      【解决方案3】:

      不是直接的答案,只是一个问题,你为什么要打字:

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

      string.Format 将对象作为 ParamArray 并在每个对象上调用 .ToString()。您使用的附加 .ToString() 仅执行创建一个不需要的新字符串。

      【讨论】:

      • 是的 - 这是不必要的,但我将它包含在我的示例中只是为了使其明确。
      【解决方案4】:

      你真的在那里设置了正确的值吗?我只问是因为您的枚举 CourseTypes 最多只能达到 2,但您说:

      Tootip 总是将枚举的整数值显示为 1,2,3...

      在你的代码中你有:

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

      您似乎正在尝试将课程 code 解析为课程 type(这可能只是表明参数名称选择不当),但可能是你的问题出在哪里。

      如果我有:

         var Course = (CourseType)3;
      

      即使枚举只变为 2,也可以编译得很好,但是当我调用 .ToString() 时,我只会得到“3”。

      【讨论】:

        【解决方案5】:

        我在使用 LINQ 时遇到了我认为相同的问题。以下代码始终返回 OfferType 枚举的数字表示。

        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();
        

        排队

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

        OfferType 始终是枚举值的数字表示,而不是预期的字符串表示。 so.OfferTypeId 的值是正确的。将行更改为

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

        将返回枚举的预期字符串表示形式。

        我不得不将演员表和 ToString 移动到另一种方法以使其工作。

        OfferType = GetOfferType(so.OfferTypeId),
        
        ...
        
        public static string GetOfferType(int id)
        {
          return ((OfferType) id).ToString();
        }
        

        【讨论】:

          【解决方案6】:

          静态Enum.GetName() 方法用于获取给定枚举值的常量名称。尝试在您的课程详情行拨打Enum.GetName(typeof(CourseType), a.CourseCode),如下所示:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-10-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多