Castle ActiveRecord / NHibernate Linq Querys with ValueTypes
Posted
by Thomas Schreiner
on Stack Overflow
See other posts from Stack Overflow
or by Thomas Schreiner
Published on 2010-05-12T10:12:14Z
Indexed on
2010/05/12
14:34 UTC
Read the original article
Hit count: 452
Given the following code for our Active Record Entites and ValueTypes Linq is not working for us.
[ActiveRecord("Person")]
public class PersonEntity : ActiveRecordLinqBase<PersonEntity>
{
string _name;
[Property("Name", Length = 20, ColumnType = "string",
Access = PropertyAccess.FieldCamelcaseUnderscore)]
public Name Name
{
get { return NameValue.Create(_name);}
set { _name = value.DataBaseValue; }
}
...
}
public abstract class Name : IValueType
{
string DataBaseValue {get;set;}
...
}
public class Namevalue : Name
{
string _name;
private NameValue(string name)
{
_name = name;
}
public static NameValue Create(string name)
{
return new NameValue(name);
}
...
}
We tried to use linq in the following way so far with no success:
var result = from PersonEntity p in PersonEntity.Queryable
where p.Name == "Thomas" select p;
return result.First(); // throws exception Cannot convert string into Name
We tried and implemented a TypeConverter for Name, but the converter never got called.
Is there a way to have linq working with this ValueTypes?
Update:
Using NHibernate.UserTypes.IUserType
it sortof works. I Implemented the Interface as described here: http://stackoverflow.com/questions/1565056/how-to-implement-correctly-iusertype
I still had to add a ConversionOperator from string to Name and had to call it Explicitly in the linq Statement, even though it was defined as implicit.
var result = from PersonEntity p in PersonEntity.Queryable
where p.Name == (Name)"Thomas" select p;
return result.First(); //Now works
© Stack Overflow or respective owner