public static IQueryable <TEntity> DynamicSearchBy <TEntity>(this IQueryable <TEntity> source, SearchModel searchModel) { var builder = new SearchExpressionBuilder(); var orderedSource = builder.ApplyToCollection(source, searchModel); return(orderedSource); }
public void TestBuild(IFilterChoice filter, string expected) { ParameterExpression paramExpression = Expression.Parameter(typeof(AppUser)); SearchExpressionBuilder builder = new SearchExpressionBuilder(); Expression result = builder.Build(filter, paramExpression); Assert.Equal(expected, result.ToString()); }
public void EqualsIgnoreCase() { var searchQueryBuilder = new SearchQueryBuilder <TestModel>() .AddComparer(x => x.Name, CompareOp.Equals, "Name"); Console.WriteLine(searchQueryBuilder.Builder); Console.WriteLine(JsonConvert.SerializeObject(searchQueryBuilder.ToSearchQuery())); SearchQuery searchQuery = searchQueryBuilder.ToSearchQuery(); IEnumerable <TestModel> testData = GetTestData(); Expression <Func <TestModel, bool> > expression = SearchExpressionBuilder.CreateSearchExpression <TestModel>(searchQuery, SearchIntent.InMemory); var result = testData.Where(expression.Compile()); AssertResultSet(result, 3, 4); }
public void String_Match_starts_and_ends_with_wildcard() { var searchQueryBuilder = new SearchQueryBuilder <TestModel>() .AddComparer(x => x.Name, CompareOp.Match, "*o*"); Console.WriteLine(searchQueryBuilder.Builder); Console.WriteLine(JsonConvert.SerializeObject(searchQueryBuilder.ToSearchQuery())); SearchQuery searchQuery = searchQueryBuilder.ToSearchQuery(); IEnumerable <TestModel> testData = GetTestData(); Expression <Func <TestModel, bool> > expression = SearchExpressionBuilder.CreateSearchExpression <TestModel>(searchQuery, SearchIntent.InMemory); var result = testData.Where(expression.Compile()); AssertResultSet(result, 2, 5, 6); }
public void Nullable_Number_is_not_null() { var searchQueryBuilder = new SearchQueryBuilder <TestModel>(LogicalOp.And) .AddComparer(x => x.NullableNumber, CompareOp.NotEquals, null); Console.WriteLine(searchQueryBuilder.Builder); Console.WriteLine(JsonConvert.SerializeObject(searchQueryBuilder.ToSearchQuery())); SearchQuery searchQuery = searchQueryBuilder.ToSearchQuery(); IEnumerable <TestModel> testData = GetTestData(); Expression <Func <TestModel, bool> > expression = SearchExpressionBuilder.CreateSearchExpression <TestModel>(searchQuery, SearchIntent.InMemory); var result = testData.Where(expression.Compile()); AssertResultSet(result, 1, 3, 5); }
public void Nullable_DateTime_interval() { var searchQueryBuilder = new SearchQueryBuilder <TestModel>(LogicalOp.And) .AddComparer(x => x.NullableTimestamp, CompareOp.GreaterThan, CreateDateTime(2001, 1, 1)); Console.WriteLine(searchQueryBuilder.Builder); Console.WriteLine(JsonConvert.SerializeObject(searchQueryBuilder.ToSearchQuery())); SearchQuery searchQuery = searchQueryBuilder.ToSearchQuery(); IEnumerable <TestModel> testData = GetTestData(); Expression <Func <TestModel, bool> > expression = SearchExpressionBuilder.CreateSearchExpression <TestModel>(searchQuery, SearchIntent.InMemory); var result = testData.Where(expression.Compile()); AssertResultSet(result, 1, 4); }
public void String_Contains() { var searchQueryBuilder = new SearchQueryBuilder <TestModel>() .AddComparer(x => x.Name, CompareOp.Contains, "o"); Console.WriteLine(searchQueryBuilder.Builder); Console.WriteLine(JsonConvert.SerializeObject(searchQueryBuilder.ToSearchQuery())); SearchQuery searchQuery = searchQueryBuilder.ToSearchQuery(); IEnumerable <TestModel> testData = GetTestData(); Expression <Func <TestModel, bool> > expression = SearchExpressionBuilder.CreateSearchExpression <TestModel>(searchQuery, SearchIntent.InMemory); var result = testData.Where(expression.Compile()); Assert.AreEqual(3, result.Count()); }
public void SimpleSearch() { var searchQueryBuilder = new SearchQueryBuilder <TestModel>(LogicalOp.Or) .AddComparer(x => x.Name, CompareOp.Equals, "Name") .AddComparer(x => x.Name, CompareOp.Equals, "name") .AddComparerGroup(LogicalOp.None) .AddComparer(x => x.Number, CompareOp.GreaterThan, 2); Console.WriteLine(searchQueryBuilder.Builder); Console.WriteLine(JsonConvert.SerializeObject(searchQueryBuilder.ToSearchQuery())); SearchQuery searchQuery = searchQueryBuilder.ToSearchQuery(); IEnumerable <TestModel> testData = GetTestData(); Expression <Func <TestModel, bool> > expression = SearchExpressionBuilder.CreateSearchExpression <TestModel>(searchQuery, SearchIntent.InMemory); var result = testData.Where(expression.Compile()); AssertResultSet(result, 3, 4, 5, 6, 7); }
public override async Task <SearchReply> ExecuteSearchRequest(SearchRequest request, ServerCallContext context) { if (!RequestIsFromLoopback(context)) { throw new Exception("Request is not from loopback"); } List <string> cns = request.UserIdentity.Cn.ToList(); List <string> ous = request.UserIdentity.Ou.ToList(); if (cns[0] != "BindUser" && ous.Count != 0) { throw new Exception("Search by Non-BindUser"); } SearchReply reply = new SearchReply { }; Guid appId = new Guid(request.UserIdentity.Dc[0]); LdapPacketParserLibrary.Parser parser = new LdapPacketParserLibrary.Parser(); LdapPacketParserLibrary.Models.LdapMessage message = parser.TryParsePacket(request.RawPacket.ToByteArray()); if (message.ProtocolOp.GetType() == typeof(LdapPacketParserLibrary.Models.Operations.Request.SearchRequest)) { LdapPacketParserLibrary.Models.Operations.Request.SearchRequest searchRequest = (LdapPacketParserLibrary.Models.Operations.Request.SearchRequest)message.ProtocolOp; int?limit = searchRequest.SizeLimit; var itemExpression = Expression.Parameter(typeof(AppUser)); SearchExpressionBuilder searchExpressionBuilder = new SearchExpressionBuilder(); var conditions = searchExpressionBuilder.Build(searchRequest.Filter, itemExpression); var queryLambda = Expression.Lambda <Func <AppUser, bool> >(conditions, itemExpression); var predicate = queryLambda.Compile(); List <AppUser> results = await _authDbContext.Users .AsNoTracking() .Include(u => u.Groups) .ThenInclude(g => g.AuthApps) .Where(queryLambda) .Where(u => u.Groups.Any(g => g.AuthApps.Any(a => a.Id == appId))) .AsSplitQuery() .ToListAsync(); SearchReply.Types.ResultEntry entry = new SearchReply.Types.ResultEntry { }; foreach (AppUser user in results) { entry.Rdn = "cn=" + user.Id.ToString() + ",ou=People,dc=" + appId; SearchReply.Types.ResultEntry.Types.ResultAttribute displayNameAttribute = new SearchReply.Types.ResultEntry.Types.ResultAttribute { Name = "displayname", }; displayNameAttribute.Value.Add(user.UserName); SearchReply.Types.ResultEntry.Types.ResultAttribute emailAttribute = new SearchReply.Types.ResultEntry.Types.ResultAttribute { Name = "email", }; emailAttribute.Value.Add(user.Email); SearchReply.Types.ResultEntry.Types.ResultAttribute objectClassAttribute = new SearchReply.Types.ResultEntry.Types.ResultAttribute { Name = "objectclass", }; objectClassAttribute.Value.Add("inetOrgPerson"); SearchReply.Types.ResultEntry.Types.ResultAttribute entryUuidAttribute = new SearchReply.Types.ResultEntry.Types.ResultAttribute { Name = "entryUuid", }; entryUuidAttribute.Value.Add(user.Id.ToString()); entry.Attributes.AddRange(new List <SearchReply.Types.ResultEntry.Types.ResultAttribute>() { displayNameAttribute, emailAttribute, objectClassAttribute, entryUuidAttribute }); reply.Results.Add(entry); } } return(reply); }