Is there any consideration when comparing Enums in LINQ-to-NHibernate or LINQ?

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;
            }//end  
            get
            {
                return _letterType;
            }//end  
        }
}

==========

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>
+3
2

enum type="int", , , , int. type, int , Linq .

, , , , . "" , . property .

<property name="LetterType" />

(2.1.2GA) NHibernate.Linq, NHibernate Core nhforge.org, .

var q = from l in session.Linq<Letter>()
    where l. LetterType == LetterType.A4
    select l;
var result = q.ToList<Letter>();

LetterType? ltype = LetterType.A4;
q = from l in session.Linq<Letter>()
    select l;
if (code != null) {
    q = q.Where( l => l.LetterType == ltype.Value );
}
result = q.ToList<Letter>();

, ltype null, ltype.Value.

q = from l in session.Linq<Letter>()
    where ltype != null && l => l.LetterType == ltype.Value
    select l;
result = q.ToList<Letter>();
+1

, Dot net Enum nhibernate Enum, .

0

Source: https://habr.com/ru/post/1734654/


All Articles