2009-07-11 26 views
3

[旗]Nhibernate的的ICriteria枚舉標誌(位掩碼)支持

public enum ShowProductOn : short 

{ 

    HomePage = 1, 

    SalesPage = 2, 

    NewsLetter = 4 

}; 

此枚舉有效值:

1 - 首頁

2 - SalesPage

3 - 首頁,SalesPage

4 - NewsLetter

5 - 首頁,新聞報

6 - SalesPage,專刊

7 - 首頁,SalesPage,專刊

我想編寫一個返回所有的產品在首頁的標準。 要檢查它在C#中是非常簡單的:

IF((MY_PARAM & ShowProductOn.HomePage)== ShowProductOn.HomePage)

Console.WriteLine("Yes"); 

在SQL它也很簡單:

DECLARE @BitMask INT = 3

IF((@BitMask & 1)= 1)

BEGIN

Print('Yes') 

END

這是NH標準,我寫回所有產品上的主頁(應該匹配1 | 3 | 5 | 7):

的ICriteria標準= NHibernateSession.CreateCriteria() .Add(Restrictions.Eq(「ShowProductOn」,ShowProductOn.HomePage));

此條件僅返回「ShowProductOn」= 1的項目,但忽略其他與「ShowProductOn」= 3 | 5 | 7匹配的項目。

有沒有人知道ICriteria/HQL語法來編寫一個條件,將返回所有項目與「ShowProductOn」= 1 | 3 | 5 | 7?

謝謝。

回答

1

我解決了這一標誌枚舉映射到字符串類型,然後使用Restrictions.Like爲quering值:

屬性定義:

public virtual DaysOfWeek Weekdays { 
    get { return (DaysOfWeek)System.Enum.Parse(typeof(DaysOfWeek), _weekdays); } 
    set { _weekdays = value.ToString(); } 
} 
private string _weekdays = "All"; 

屬性映射(使用HBM 。xml文件)

<property name="Weekdays" column="WEEKDAYS" access="field.camelcase-underscore" not-null="false" /> 

最後的條件查詢:

Restrictions.Like("Weekdays", ConvertToDaysOfWeek(time.DayOfWeek).ToString(), MatchMode.Anywhere); 

希望這有助於。