Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Use PredicateBuilder to search for partial matches for keywords in an entity property based on this answer - http://stackoverflow.com/a/14349896/201648
//http://arbel.net/2008/08/18/writing-methods-and-classes-in-linqpad/
//Choose "C# Statement(s)" in the Type drop-down.
//Write the code that LINQPad should execute.
//Put a closing curly-bracket ("}") at the end.
//Write as many classes and methods as you like.
//On the last class/method, omit closing curly-bracket.
//To remove PredicateBuilder errors, see http://stackoverflow.com/a/1511046
var Names = new List<Name> {
new Name {
Company = "Test Soft"
},
new Name {
Company = "Dance Like a Monkey"
},
new Name {
Company = "Test widgets"
},
new Name {
Company = "Monkey Soft"
}
}
.AsQueryable();
var predicate = PredicateBuilder.False<Name>();
List<string> names = new List<string>();
names.Add("Soft");
names.Add("Test");
foreach(string name in names)
{
predicate = predicate.Or(n => n.Company.Contains(name));
}
var results = (from n in Names
.AsExpandable()
.Where(predicate)
select(new{ n.Company}));
System.Console.WriteLine(results);
}
public class Name
{
public string Company { get; set; }
//} //This is deliberately left off so that it will work as a Linqpad statement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment