2016-07-27 42 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()