Skip to content

Instantly share code, notes, and snippets.

@ODataTeam
Created September 15, 2014 06:53
Show Gist options
  • Save ODataTeam/5245d34e73d2bfe6a3e0 to your computer and use it in GitHub Desktop.
Save ODataTeam/5245d34e73d2bfe6a3e0 to your computer and use it in GitHub Desktop.
class StringRepResolver : ODataUriResolver
{
public override void PromoteBinaryOperandTypes(
BinaryOperatorKind binaryOperatorKind,
ref SingleValueNode leftNode,
ref SingleValueNode rightNode,
out IEdmTypeReference typeReference)
{
if (binaryOperatorKind == BinaryOperatorKind.Multiply
&& leftNode.TypeReference != null
&& leftNode.TypeReference.IsString()
&& rightNode.TypeReference != null
&& rightNode.TypeReference.IsInt32())
{
// The result type should be Edm.String, as it could be nullable or not, we just took the left
// node's type reference.
typeReference = leftNode.TypeReference;
return;
}
// fallback
base.PromoteBinaryOperandTypes(binaryOperatorKind, ref leftNode, ref rightNode, out typeReference);
}
}
// Usage of StringRepResolver
var parser = new ODataUriParser(
extModel.Model,
ServiceRoot,
new Uri("http://demo/odata.svc/People?$filter=Name eq 'Red' mul 3"));
try
{
// Should throw exception
var clause = parser.ParseFilter();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
var parser2 = new ODataUriParser(
extModel.Model,
ServiceRoot,
new Uri("http://demo/odata.svc/People?$filter=Name eq 'Red' mul 3"))
{
Resolver = new StringRepResolver()
};
// Should work
var clause2 = parser2.ParseFilter();
Console.WriteLine(clause2.Expression.ToLogString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment