Skip to content

Instantly share code, notes, and snippets.

@hungdv136
Created December 2, 2017 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hungdv136/94ba0e77340ff3b4821b077ec91de2d9 to your computer and use it in GitHub Desktop.
Save hungdv136/94ba0e77340ff3b4821b077ec91de2d9 to your computer and use it in GitHub Desktop.
Translator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators;
using StudyEfCore.Expressions;
using StudyEfCore.Utilities;
namespace StudyEfCore.Translators
{
public class FtsTranslator : IMethodCallTranslator
{
private static readonly MethodInfo ContainsMethodInfo
= typeof(SqlFunctions).GetMethod(nameof(SqlFunctions.Contains), new[] { typeof(string), typeof(string) });
private static readonly MethodInfo FreetextMethodInfo
= typeof(SqlFunctions).GetMethod(nameof(SqlFunctions.Freetext), new[] { typeof(string), typeof(string) });
public virtual Expression Translate(MethodCallExpression expr)
=> Equals(expr.Method, ContainsMethodInfo)
? new FtsExpression("CONTAINS", new[] { expr.Arguments.First(), expr.Arguments.Last() })
: Equals(expr.Method, FreetextMethodInfo)
? new FtsExpression("FREETEXT", new[] { expr.Arguments.First(), expr.Arguments.Last() })
: null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment