In such a request:
var q = from l in session.Linq<Letter>()
where
letterTypeSearch == null ? true :
(l.LetterType.ToString() == letterTypeSearch)
l.LetterType is Enum.
UPDATE
It seems that it is not possible to compare Enums in the current linq-to-nhibernate. Although letterTypeSearch is a string containing an instance LetterTypethat ToString()ed is LetterTypeinherited from int, there are 3 ways to compare:
1- Comparison in String: This is not possible because it l.LetterType.ToString()produces "(ArgumentOutOfRangeException): The index was out of range. It must be non-negative and smaller than the size of the collection. Parameter name: index," error.
2- Comparison in Enum( LetterType): this is impossible because it l.LetterType == LetterType.Internalleads to "(QueryException): Type of mismatch in NHibernate.Criterion.SimpleExpression: LetterType expected type System.Int32, actual type Faraconesh.EnterpriseAppUnits.OfficeAutomation.BusinessEntities.LetterType,".
3- Comparison in Int32: still impossible, because it Convert.ToInt32(l.LetterType)creates a "(NotImplementedException): ToInt32 method is not implemented.," Error.
So how can I compare Enums in LINQ-to-NHibernate? Is this problem specific to LINQ-to-NHibernate, or is all LINQ users having this problem?
UPDATE2
here is the class, enumeration and mapping (smmarized):
public class Letter
{
private LetterType _letterType;
public LetterType LetterType
{
set
{
_letterType = value;
}
get
{
return _letterType;
}
}
}
==========
public enum LetterType {Inbound = 0, Outbound = 1, Inner = 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>