This code works fine:
ICriteria criteria = GetSession().CreateCriteria<MyClass>();
criteria.Add(Restrictions.Where<MyClass>(x => x.Field1 >= myVariable));
But the following code does not work:
criteria.Add(Restrictions
.Where<MyClass>(x =>
(x.Field1 +
x.Field2 +
x.Field3 +
x.Field4) >= myVariable));
The above code gets this error on execution:
A variable 'x' of type 'myClass' referencing the scope '', but this is not defined
Please help (sorry for my poor English).
Sarah
Change 1
My workaround:
var result = criteria.List<MyClass>();
result.Where(x => (x.Field1 + x.Field2 + x.Field3 + x.Field4 >= myVariable));
and this work. I would rather put the Where clause before the choice ...
Edit 2
Final solution (as suggested from @mhoff):
var result = criteria.List<MyClass>();
result.Where(x => this.GetSum(x) >= myVariable);
... do something ...
... ToList()
private int GetSum(MyClass x) {
return (x.Field1 + x.Field2 + x.Field3 + x.Field4);
}
source
share