Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. using AutoMapper;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Converters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. using System.Reflection;
  9. using System.Security.Cryptography;
  10.  
  11. namespace ConsoleApp35
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             var test = new Searcher("test");
  18.             var query = new List<Account>() { new Account("ZBAGI", 1), new Account("Test", 2) }.AsQueryable();
  19.             var ttt = test.Search(query, (a => a.Name), (a => a.Id)).FirstOrDefault();
  20.  
  21.             Console.ReadLine();
  22.         }
  23.         public class Account
  24.         {
  25.             public Account(string name, int id)
  26.             {
  27.                 Name = name;
  28.                 Id = id;
  29.             }
  30.             public string Name { get; set; }
  31.             public int Id { get; set; }
  32.         }
  33.  
  34.         public class Searcher
  35.         {
  36.             public string RawQuery { get; }
  37.  
  38.             public Searcher(string query)
  39.             {
  40.                 RawQuery = query;
  41.             }
  42.  
  43.             public IQueryable<T> Search<T>(IQueryable<T> query, params Expression<Func<T, object>>[] memberSelectors)
  44.             {
  45.                 // Generate constant phrases expression
  46.                 var phrasesGroups = new List<Expression>();
  47.                 foreach (var groups in RawQuery.Split("|"))
  48.                 {
  49.                     var phrasesExpression = new List<Expression>();
  50.                     foreach (var rawPhrase in groups.Split(" "))
  51.                     {
  52.                         phrasesExpression.Add(Expression.Constant(rawPhrase));
  53.                     }
  54.  
  55.                     phrasesGroups.Add(Expression.NewArrayInit(typeof(string), phrasesExpression));
  56.                 }
  57.                 var phrasesGroupsExpression = Expression.NewArrayInit(typeof(string[]), phrasesGroups);
  58.                 ////
  59.  
  60.                 var phrases = Expression.Parameter(typeof(string[]), "phrases");
  61.                 var phrase = Expression.Parameter(typeof(string), "phrase");
  62.                 var entity = Expression.Parameter(typeof(T), "entity");
  63.                 var memberSelector = Expression.Parameter(typeof(Expression<Func<T, object>>), "memberSelector");
  64.  
  65.                 var condition = Expression.AndAlso(
  66.                                     Expression.NotEqual(Expression.Invoke(memberSelector, entity), Expression.Constant(null)),
  67.                                      Expression.Call( Expression.Call(Expression.Invoke(memberSelector, entity), typeof(object).GetMethod("ToString")) , typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), phrase)
  68.                                  );
  69.  
  70.                 var memberSelectorsExpression = Expression.Lambda<Func<Expression<Func<T, object>>, bool>>(condition, memberSelector);
  71.  
  72.                 Func<IEnumerable<Expression<Func<T, object>>>, Func<Expression<Func<T, object>>, bool>, bool> anyMemberSelector = Enumerable.Any;
  73.                 Func<IEnumerable<string[]>, Func<string[], bool>, bool> anyList = Enumerable.Any;
  74.  
  75.                 var anyPhrase = Expression.Lambda<Func<string, bool>>(Expression.Call(null, anyMemberSelector.Method, Expression.NewArrayInit(typeof(Expression<Func<T, object>>), memberSelectors), memberSelectorsExpression), phrase);
  76.                 var allPhrases = Expression.Lambda<Func<string[], bool>>(Expression.Call(null, typeof(Enumerable).GetMethod("All").MakeGenericMethod(typeof(string)), phrases, anyPhrase), phrases);
  77.  
  78.                 var whereCondition = Expression.Lambda<Func<T, bool>>(Expression.Call(null, anyList.Method, phrasesGroupsExpression, allPhrases), entity);
  79.  
  80.                 Console.WriteLine(whereCondition.ToString());
  81.                 return query.Where(whereCondition);
  82.             }
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement