In version 17.1, we have improved error detection in XPQuery when non-persistent properties are used in it. Previously, such queries didn't work (either failed with various exceptions or worked incorrectly) and thus there were hidden errors in your programs. Now, we throw an exception that describes which non-persistent properties are used in XPQuery to help diagnosing these errors. If such properties are used intentionally, consider implementing them using PersistentAliasAttribute or adding the ToList method call to explicitly process these properties on the client side.
For example, the following code now throws an exception:
C#public class Person: XPObject {
string _firstName;
public string FirstName {
get { return _firstName; }
set { SetPropertyValue("FirstName", ref _firstName, value); }
}
string _lastName;
public string LastName {
get { return _lastName; }
set { SetPropertyValue("LastName", ref _lastName, value); }
}
public string NonPersistentProperty {
get {
return FirstName + " " + LastName;
}
}
}
//...
var query = new XPQuery<Person>(uow).Where(p => p.FirstName == "John" && p.NonPersistentProperty.Contains("Jr"))
Visual BasicPublic Class Person
Inherits XPObject
Private _firstName As String
Public Property FirstName() As String
Get
Return _firstName
End Get
Set(ByVal value As String)
SetPropertyValue("FirstName", _firstName, value)
End Set
End Property
Private _lastName As String
Public Property LastName() As String
Get
Return _lastName
End Get
Set(ByVal value As String)
SetPropertyValue("LastName", _lastName, value)
End Set
End Property
Public ReadOnly Property NonPersistentProperty() As String
Get
Return FirstName & " " & LastName
End Get
End Property
End Class
'...
Dim query = (New XPQuery(Of Person)(uow)).Where(Function(p) p.FirstName = "John" AndAlso p.NonPersistentProperty.Contains("Jr"))
It can be fixed by rewriting the query with the ToList method to process a part of the query on the client side:
C#var query = new XPQuery<Person>(uow).Where(p => p.FirstName == "John").ToList().Where(p => p.NonPersistentProperty.Contains("Jr"));
Visual BasicDim query = (New XPQuery(Of Person)(uow)).Where(Function(p) p.FirstName = "John").ToList().Where(Function(p) p.NonPersistentProperty.Contains("Jr"))
It can also be fixed by implementing the property using PersistentAliasAttribute:
C#public class Person : XPObject {
//...
[PersistentAlias("Concat(FirstName, ' ', LastName")]
public string NonPersistentProperty { // It is persistent now
get {
return (string)EvaluateAlias("NonPersistentProperty");
}
}
}
Visual BasicPublic Class Person
Inherits XPObject
'...
<PersistentAlias("Concat(FirstName, ' ', LastName")>
Public ReadOnly Property NonPersistentProperty() As String ' It is persistent now
Get
Return CStr(EvaluateAlias("NonPersistentProperty"))
End Get
End Property
End Class
Since rewriting many queries in an existing application can be a tedious task, starting with version 17.1.6 we implemented the static XPQueryBase.SuppressNonPersistentPropertiesCheck property. Set this property to True (at the application start) to revert to the old behavior. Note that when this property is set, an exception still can be thrown in scenarios that were never supported, but at a later stage.
Search keywords: NotSupportedException, InvalidOperationException, LINQ, 17.1, XPQuery<T>, "The expression is not supported in the Where/OrderByXXX clause"