Author |
Share Topic Topic Search Topic Options
|
monkeyking
Groupie
Joined: 04-Mar-2009
Location: brisbane
Posts: 68
|
Post Options
Quote Reply
Topic: Linq Query question 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
|
|
GregD
IdeaBlade
Joined: 09-May-2007
Posts: 374
|
Post Options
Quote Reply
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); } }
|
|
GregD
IdeaBlade
Joined: 09-May-2007
Posts: 374
|
Post Options
Quote Reply
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(); }
|
|
monkeyking
Groupie
Joined: 04-Mar-2009
Location: brisbane
Posts: 68
|
Post Options
Quote Reply
Posted: 17-Sep-2009 at 6:17pm |
thanks GregD, that's helpful
|
|
monkeyking
Groupie
Joined: 04-Mar-2009
Location: brisbane
Posts: 68
|
Post Options
Quote Reply
Posted: 17-Sep-2009 at 8:42pm |
Hey, GregD, can you please tell me what i should import to get the PredicateBuilder class?
|
|
GregD
IdeaBlade
Joined: 09-May-2007
Posts: 374
|
Post Options
Quote Reply
Posted: 23-Dec-2009 at 10:43am |
IdeaBlade.Linq
|
|