public
Last active

Use PredicateBuilder to search for partial matches for keywords in an entity property based on this answer - http://stackoverflow.com/a/14349896/201648

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
//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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.