I'm trying to do that :
public class SomeEntityClass
{
public Guid MyClassProperty {get;set;}
}
public class AnotherEntityClass
{
public Guid AnotherProperty {get;set;}
}
public T GetByProperty<T>(Guid value, Expression<Func<T, object>> selector)
{
return = Session.Query<T>().Where(x => selector == value).FirstOrDefault();
}
Should be called :
Repository.GetByProperty<SomeEntityClass>(Guid.NewGuid(), x => x.MyClassProperty );
Repository.GetByProperty<AnotherEntityClass>(Guid.NewGuid(), x => x.AnotherProperty);
but it doesn't work.
Any help ?
Thanks.
Try to use something like that:
public T GetByProperty<T, TValue>(TValue value, Expression<Func<T, TValue>> selector) {
var predicate = Expression.Lambda<Func<T, bool>>(
Expression.Equal(selector.Body, Expression.Constant(value)),
selector.Parameters
);
return Session.Query<T>().Where(predicate).FirstOrDefault();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With