我正在尝试这样做:
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();
}
应该被称为:
Repository.GetByProperty<SomeEntityClass>(Guid.NewGuid(), x => x.MyClassProperty );
Repository.GetByProperty<AnotherEntityClass>(Guid.NewGuid(), x => x.AnotherProperty);
但它不起作用。
有什么帮助吗?
Thanks.
尝试使用类似的东西:
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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)