1234567891011121314151617181920212223242526272829303132 |
- using System;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using Microsoft.EntityFrameworkCore.Query.Expressions;
- using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators;
-
- namespace Luticate2.Utils.DataAccess.Npgsql
- {
- public class NpgsqlIntToStringTranslator : IMethodCallTranslator
- {
- private readonly Type[] _declaringTypes = {typeof(int), typeof(int?)};
-
- private readonly string _clrMethodName = nameof(ToString);
-
- public Expression Translate(MethodCallExpression methodCallExpression)
- {
- foreach (var declaringType in _declaringTypes)
- {
- var methodInfo = declaringType.GetTypeInfo()
- .GetDeclaredMethods(_clrMethodName).SingleOrDefault(m => !m.GetParameters().Any());
- if (methodInfo != null && methodCallExpression.Method.Name == _clrMethodName
- && declaringType.IsAssignableFrom(methodCallExpression.Object.Type))
- {
- return new ExplicitCastExpression(methodCallExpression.Object, typeof(string));
- }
- }
-
- return null;
- }
- }
- }
|