| Linq Query question
 
 Printed From: IdeaBlade
 Category:  DevForce
 Forum Name:  DevForce 2009
 Forum Discription:  For .NET 3.5
 URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1462
 Printed Date: 31-Oct-2025 at 12:22pm
 
 
 Topic: Linq Query question
 Posted By: monkeyking
 Subject: Linq Query question
 Date Posted: 09-Sep-2009 at 11:00pm
 
 
        
          | i have a list of stock names, let's call it 'stockNames' (List<string> stockNames) and i have a 'Stock' table, which are a set of stock entities. now I want to use Linq to query the entities which their names are listed in 'stockName' list. 
 I used query as that, 'var query = entityManager.Stocks.where(o=>(stockNames.Contains(o.name)));'. but List<string>.Contains(string) can't be used in the Linq 'where' clause. is there any other way to resolve this?
 
 Regards
 John
 
 |  
 
 Replies:
 Posted By: GregD
 Date Posted: 10-Sep-2009 at 2:07pm
 
 
        
          | You can do this with the DevForce PredicateBuilder. I'll show you that in a second, but just to cover the other alternatives: 
 1. You can bring the complete list of entities to the client and filter it there:
 
 List<string> lastNames = new List<string>();
 lastNames.Add("Davolio");
 lastNames.Add("Fuller");
 lastNames.Add("Buchanan");
 var employees = _mgr.Employees.ToList().Where(e => lastNames.Contains(e.LastName));
 
 2.   You can use a Remote Service Method call to do the above on the server, then ship down only the results;
 
 3.   You can hand-code the query with lots of ORs in the Where clause
 
 List<Employee> employees = _mgr.Employees.Where(
 e => e.LastName == "Davolio" ||
 e.LastName == "Fuller" ||
 e.LastName == "Buchanan").ToList();
 
 Choices 1 and 3, of course, are either impossible or unacceptable in many scenarios; and choice 2 is maybe more trouble than you'd like to go to. So here's how you do it with the PredicateBuilder, which basically automates the process of  constructing a query like #3:
 
 private void DoItUsingPredicateBuilder() {
 List<string> lastNames = new List<string>();
 lastNames.Add("Davolio");
 lastNames.Add("Fuller");
 lastNames.Add("Buchanan");
 
 var tests = EmployeeLastNameTests(lastNames).ToArray();
 if (0 == tests.Length) return;
 var employeeLastNamePredicate = PredicateBuilder.Or(tests);
 List<Employee> employees = _mgr.Employees.Where(employeeLastNamePredicate).ToList();
 
 //foreach (Employee anEmployee in employees) {
 //  Console.WriteLine(anEmployee.LastName);
 //}
 //PromptToContinue();
 }
 
 private IEnumerable<Expression<Func<Employee, bool>>> EmployeeLastNameTests(IEnumerable<String> words) {
 foreach (var each in words) {
 var word = each;
 yield return e => e.LastName.Contains(word);
 }
 }
 
 
 |  
 Posted By: GregD
 Date Posted: 10-Sep-2009 at 3:07pm
 
 
        
          | Here's a second variation of the PredicateBuilder approach: 
 private void DoItUsingPredicateBuilder() {
 Console.WriteLine(".....Use the PredicateBuilder...");
 List<string> lastNames = new string[] {"Davolio", "Fuller", "Buchanan"}.ToList();
 var expr = PredicateBuilder.False<Employee>();
 foreach (string lastName in lastNames) {
 // Make a new variable for this iteration's lastName value; otherwise,
 // because of delayed execution, all expressions constructed below
 // will use the last-encountered version of lastName.
 string lastNameThisIteration = lastName;
 expr = expr.Or(e => e.LastName == lastNameThisIteration);
 }
 List<Employee> employees = _mgr.Employees.Where(expr).ToList();
 
 //foreach (Employee anEmployee in employees) {
 //  Console.WriteLine(anEmployee.LastName);
 //}
 //PromptToContinue();
 }
 
 
 |  
 Posted By: monkeyking
 Date Posted: 17-Sep-2009 at 6:17pm
 
 
        
          | thanks GregD, that's helpful |  
 Posted By: monkeyking
 Date Posted: 17-Sep-2009 at 8:42pm
 
 
        
          | Hey, GregD, can you please tell me what i should import to get the PredicateBuilder class? |  
 Posted By: GregD
 Date Posted: 23-Dec-2009 at 10:43am
 
 
 
 |