Created
December 2, 2017 04:04
-
-
Save hungdv136/94ba0e77340ff3b4821b077ec91de2d9 to your computer and use it in GitHub Desktop.
Translator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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