2016-07-27 62 views
0

如何在LINQ查询中获取显示/描述枚举?获取显示在LINQ中的枚举

例如

var query = dbo.Records.AsQueryable() 
       .Select(x => new 
       { 
        Id = x.Id, 
        Care = new 
        { 
         x.StartDate, 
         x.ForwardedBy, 
        }, 
        Prescriptions = x.Prescriptions.Select(p => new 
        { 
         p.Medicament, 
         p.IntervalUse //My Enum, how get Display name? 
        }), 
       }).OrderByDescending(x => x.Id); 

此查询是一个exampleand我需要AsQueryable在我的数据库

+0

任何原因,你不能只把它作为'enum'并获得显示名称在视图或您的形式?这通常是首选的方法。 –

+1

是什么意思显示/描述?你在你的枚举上有[DisplayNameAttribute] s,还是你在谈论枚举的名字 –

+0

@JoshK我回到json – user98498

回答

0

我会用System.Reflection库来产生更快的查询。

那不是在LINQ to实体支持
public static GetMyEnumDisplayName(MyEnumType value) 
{ 
    var displayAttribute = value.GetType() 
     .GetTypeInfo() 
     .GetDeclaredField(result) 
     .GetCustomAttribute(typeof(DisplayAttribute)); 

     if (displayAttribute != null) 
     { 
      var Name = ((DisplayAttribute)displayAttribute).Name; 
     } 
} 

,但你可以做,而不是在您的视图模型的辅助属性。

你的第一步是选择一个视图模型,而不是一个匿名对象 下一步是在二传手

public string EnumDisplayValue { get { return GetMyEnumDisplayName(MyEnumValue); } } 
+0

我如何在linq查询中使用? – user98498

+0

@ user98498将该代码转换为方法并从linq查询中调用该方法 –

+0

不工作Sam ... – user98498

1

由于LINQ不知道这些扩展方法增加一个功能,你会必须先枚举,然后使用反射来获取属性。

public static class EnumExtensions 
{ 
    public static string GetDisplayName(this Enum value) 
    { 
     var attribute = (DisplayNameAttribute) value.GetType() 
      .GetField(value.ToString()) 
      .GetCustomAttributes(false) 
      .Where(a => a is DisplayNameAttribute) 
      .FirstOrDefault(); 

     return attribute != null ? attribute.DisplayName : value.ToString(); 
    } 
} 

我现在不能测试此权利,但你可能会试试这个,让我们知道,如果它的工作原理:

var query = dbo.Records.Include(x => x.Prescriptions).OrderByDescending(x => x.Id).AsEnumerable() 
      .Select(x => new 
      { 
       Id = x.Id, 
       Care = new 
       { 
        x.StartDate, 
        x.ForwardedBy, 
       }, 
       Prescriptions = x.Prescriptions.Select(p => new 
       { 
        p.Medicament, 
        p.IntervalUse.GetDisplayName() 
       }), 
      }); 
+0

我如何在LINQ查询中使用? – user98498

+0

您不能在LINQ查询中使用它,除非您已经枚举了'IQueryable',因为LINQ to Entities中没有映射。 –

+0

Wtf?为什么有enum支持,如果没有支持linq枚举? :| – user98498

0

它可能不是一个坏主意,如果写的enum扩展方法这是你会需要经常的东西:

public static string Describe(this Enum enumVal) 
{ 
    var type = enumVal.GetType(); 
    var memInfo = type.GetMember(enumVal.ToString()); 
    var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false); 
    return (attributes.Length > 0) ? ((DescriptionAttribute)(attributes[0])).Description : enumVal.ToString(); 
} 
0

你为什么不走了,最简单的方法:

public enum IntervalUse 
    { 
     Hourly, 
     Daily, 
     Weekly 
    } 

    public static class EnumExt 
    { 
     public static string GetDescription(this IntervalUse item) 
     { 
      switch (item) 
      { 
       case IntervalUse.Hourly: 
        return "Hour by hour"; 
       case IntervalUse.Daily: 
        return "Day by day"; 
       case IntervalUse.Weekly: 
        return "Each week ..."; 
       default: 
        throw new ArgumentOutOfRangeException(nameof(item), item, null); 
      } 
     } 
    } 

那么您可以在您的查询拨打:p.IntervalUse.GetDescription()