В таком запросе:
var q = from l in session.Linq<Letter>()
where
letterTypeSearch == null ? true :
(l.LetterType.ToString() == letterTypeSearch)
l.LetterType - это Enum.
UPDATE
Кажется, невозможно сравнить Enums в текущем linq-to-nhibernate. Хотя letterTypeSearch является строкой, содержащей экземпляр LetterType
, который ToString()
ed, а LetterType
наследуется от int
, существует 3 способа сравнения:
1- Сравнение в String
: невозможно, поскольку l.LetterType.ToString()
производит "(ArgumentOutOfRangeException): индекс вышел за пределы диапазона. Должен быть неотрицательным и меньше размера коллекции.
Имя параметра: индекс, «ошибка».
2- Сравнение в самом Enum
(LetterType
): это невозможно, потому что l.LetterType == LetterType.Internal
приводит к "(QueryException): несоответствие типов в NHibernate.Criterion.SimpleExpression: ожидаемый тип LetterType System.Int32, фактический тип Faraconesh.EnterpriseAppUnits.OfficeAutomation.BusinessEntities.LetterType,
"ошибка.
3- Сравнение в Int32
: пока невозможно, поскольку Convert.ToInt32(l.LetterType)
генерирует "(NotImplementedException): метод ToInt32 не реализован.," Ошибка.
Так как я могу сравнить Enums в LINQ-to-NHibernate? Эта проблема характерна для LINQ-to-NHibernate или у всех пользователей LINQ такая проблема?
UPDATE2
вот класс, enum и mapping (сммализованный):
public class Letter
{
private LetterType _letterType;
public LetterType LetterType
{
set
{
_letterType = value;
}//end
get
{
return _letterType;
}//end
}
}
=========
public enum LetterType
{
Входящий = 0,
Исходящий = 1,
Внутренний = 2,
}
=========
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class
name="Faraconesh.EnterpriseAppUnits.OfficeAutomation.BusinessEntities.Letter,Faraconesh.EnterpriseAppUnits.OfficeAutomation.BusinessEntities"
table="OfficeAutomation_Letter">
<property
name="LetterType" column="LetterType"
type="int" update="true" insert="true" access="property"
not-null="true"/>
</class>
</hibernate-mapping>