Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using AutoMapper;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Converters;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using System.Security.Cryptography;
- namespace ConsoleApp35
- {
- class Program
- {
- static void Main(string[] args)
- {
- var test = new Searcher("test");
- var query = new List<Account>() { new Account("ZBAGI", 1), new Account("Test", 2) }.AsQueryable();
- var ttt = test.Search(query, (a => a.Name), (a => a.Id)).FirstOrDefault();
- Console.ReadLine();
- }
- public class Account
- {
- public Account(string name, int id)
- {
- Name = name;
- Id = id;
- }
- public string Name { get; set; }
- public int Id { get; set; }
- }
- public class Searcher
- {
- public string RawQuery { get; }
- public Searcher(string query)
- {
- RawQuery = query;
- }
- public IQueryable<T> Search<T>(IQueryable<T> query, params Expression<Func<T, object>>[] memberSelectors)
- {
- // Generate constant phrases expression
- var phrasesGroups = new List<Expression>();
- foreach (var groups in RawQuery.Split("|"))
- {
- var phrasesExpression = new List<Expression>();
- foreach (var rawPhrase in groups.Split(" "))
- {
- phrasesExpression.Add(Expression.Constant(rawPhrase));
- }
- phrasesGroups.Add(Expression.NewArrayInit(typeof(string), phrasesExpression));
- }
- var phrasesGroupsExpression = Expression.NewArrayInit(typeof(string[]), phrasesGroups);
- ////
- var phrases = Expression.Parameter(typeof(string[]), "phrases");
- var phrase = Expression.Parameter(typeof(string), "phrase");
- var entity = Expression.Parameter(typeof(T), "entity");
- var memberSelector = Expression.Parameter(typeof(Expression<Func<T, object>>), "memberSelector");
- var condition = Expression.AndAlso(
- Expression.NotEqual(Expression.Invoke(memberSelector, entity), Expression.Constant(null)),
- Expression.Call( Expression.Call(Expression.Invoke(memberSelector, entity), typeof(object).GetMethod("ToString")) , typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), phrase)
- );
- var memberSelectorsExpression = Expression.Lambda<Func<Expression<Func<T, object>>, bool>>(condition, memberSelector);
- Func<IEnumerable<Expression<Func<T, object>>>, Func<Expression<Func<T, object>>, bool>, bool> anyMemberSelector = Enumerable.Any;
- Func<IEnumerable<string[]>, Func<string[], bool>, bool> anyList = Enumerable.Any;
- var anyPhrase = Expression.Lambda<Func<string, bool>>(Expression.Call(null, anyMemberSelector.Method, Expression.NewArrayInit(typeof(Expression<Func<T, object>>), memberSelectors), memberSelectorsExpression), phrase);
- var allPhrases = Expression.Lambda<Func<string[], bool>>(Expression.Call(null, typeof(Enumerable).GetMethod("All").MakeGenericMethod(typeof(string)), phrases, anyPhrase), phrases);
- var whereCondition = Expression.Lambda<Func<T, bool>>(Expression.Call(null, anyList.Method, phrasesGroupsExpression, allPhrases), entity);
- Console.WriteLine(whereCondition.ToString());
- return query.Where(whereCondition);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement