Skip to content

Instantly share code, notes, and snippets.

@pjvds
Created January 21, 2011 14:59
Show Gist options
  • Save pjvds/789779 to your computer and use it in GitHub Desktop.
Save pjvds/789779 to your computer and use it in GitHub Desktop.
Possible solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using LinqKit;
using System.Text.RegularExpressions;
namespace DynamicQueryProblem
{
public class PersonRepository
{
private FakeEntityModel _entities = new FakeEntityModel();
/// <summary>
/// Zoekmethode die aangeroepen word vanuit onze webpagina en de resultaten vervolgens
/// in de grid weergeeft
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public List<Person> Search(string query)
{
var keywords = query.ToLower().Split(' ');
return Search(keywords);
}
public List<Person> Search(params string[] keywords)
{
// ____. _______
// | | ____ __ ____ _ __ \ \ _____ _____ _____
// | |/ _ \| | \ \/ \/ / / | \\__ \ \__ \ / \
//\__| ( <_> ) | /\ / / | \/ __ \_/ __ \| Y Y \
//\________|\____/|____/ \/\_/ \____|__ (____ (____ /__|_| /
// \/ \/ \/ \/
//___________ .__ .__ ___________.__.__ .__ .___
//\_ _____/_ __| | | | \_ _____/|__| | | | ____ __| _/
// | __)| | \ | | | | __) | | | | | _/ __ \ / __ |
// | \ | | / |_| |__| \ | | |_| |_\ ___// /_/ |
// \___ / |____/|____/____/\___ / |__|____/____/\___ >____ |
// \/ \/ \/ \/
//___________.__ .____ .__
//\__ ___/| |__ ____ | | |__| ____ ______
// | | | | \_/ __ \ | | | |/ \ / ____/
// | | | Y \ ___/ | |___| | | < <_| |
// |____| |___| /\___ > |_______ \__|___| /\__ |
// \/ \/ \/ \/ |__|
// _____ __
// / \ _____ _______/ |_ ___________
// / \ / \\__ \ / ___/\ __\/ __ \_ __ \
// / Y \/ __ \_\___ \ | | \ ___/| | \/
//\____|__ (____ /____ > |__| \___ >__|
// \/ \/ \/ \/
//________ __
//\_____ \ __ __ ____ _______/ |_
// / / \ \| | \_/ __ \ / ___/\ __\
// / \_/. \ | /\ ___/ \___ \ | |
// \_____\ \_/____/ \___ >____ > |__|
// \__> \/ \/
var predicate = PredicateBuilder.True<Person>();
var expressions = FullTextSearchExpressionForPerson(keywords);
predicate = expressions.Aggregate(predicate, (current, exp) => current.And(exp));
List<Person> searchresult = this.GetPersons(predicate);
return searchresult;
}
protected Expression<Func<Person, bool>> FullTextSearchExpressionForPerson(string keyword)
{
var keywordCopy = keyword;
return (p => p.Name.ToLower().Contains(keywordCopy) ||
p.Initials.ToLower().Contains(keywordCopy) ||
p.Address.ToLower().Contains(keywordCopy) ||
p.Remark.ToLower().Contains(keywordCopy));
}
protected IEnumerable<Expression<Func<Person, bool>>> FullTextSearchExpressionForPerson(string[] keywords)
{
foreach(var keyword in keywords)
{
var keywordCopy = keyword;
yield return FullTextSearchExpressionForPerson(keywordCopy);
}
}
public IQueryable<Person> Query()
{
return _entities.Persons;
}
public List<Person> GetPersons(Expression<Func<Person, bool>> predicate)
{
return _entities.Persons.Where(predicate).ToList();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment