You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NpgsqlIntToStringTranslator.cs 1.2KB

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. using Microsoft.EntityFrameworkCore.Query.Expressions;
  6. using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators;
  7. namespace Luticate2.Utils.DataAccess.Npgsql
  8. {
  9. public class NpgsqlIntToStringTranslator : IMethodCallTranslator
  10. {
  11. private readonly Type[] _declaringTypes = {typeof(int), typeof(int?)};
  12. private readonly string _clrMethodName = nameof(ToString);
  13. public Expression Translate(MethodCallExpression methodCallExpression)
  14. {
  15. foreach (var declaringType in _declaringTypes)
  16. {
  17. var methodInfo = declaringType.GetTypeInfo()
  18. .GetDeclaredMethods(_clrMethodName).SingleOrDefault(m => !m.GetParameters().Any());
  19. if (methodInfo != null && methodCallExpression.Method.Name == _clrMethodName
  20. && declaringType.IsAssignableFrom(methodCallExpression.Object.Type))
  21. {
  22. return new ExplicitCastExpression(methodCallExpression.Object, typeof(string));
  23. }
  24. }
  25. return null;
  26. }
  27. }
  28. }