
|
I have the requirements to change a method, in particular a LINQ query to return results on various permutations of 2 variables (Postcode, Surname) which would result in changing the where clause as shown in the highlighted row below. But instead of re-writing the statement for the 3 different where clauses I want to be able to reuse as much as possible.
var PeopleRecordsSearch = from SomePeople in Person()
join SomeClient in Client() on SomePeople.PersonID equals SomeClient.PersonID
where SomePeople.Surname.Contains(Surname) && SomePeople.Surname.Length > 0 && SomeClient.Postcode == Postcode
select SomePeople;
I have looked at using the Func which works great when the results (i.e. the select statement) are the same as the in variable i.e.
Func<Tables.Person, bool> WhereSurname = p => p.Surname = Surname;
which I can run then as
PeopleRecordSearch.Where(WhereSurname).ToList();
but if I want to search the Postcode of the Client table with this
Func<Tables.Client, bool> WherePostcode = p => p.Postcode == Postcode;
I get the following error in Visual Studio
Argument 2: cannot convert from 'System.Func' to 'System.Linq.Expressions.Expression>' Person.cs 125 69
Which I know is the Func statement.
My question is, is there a way that I can re use the main body of the query and write a dynamic where clause into the statement or the results by doing PeopleRecordSearch.Where(FuncArgument).ToList();?
Thanks
Simon
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|

|
Simon, this is your lucky day. First of all, let's change your initial query like this:var PeopleRecordsSearch = from SomePeople in Person()
join SomeClient in Client() on SomePeople.PersonID equals SomeClient.PersonID
select SomePeople;Now, to search on the postcode, do this:var postcodeSearch = (from people in PeopleRecordsSearch
where people.Postcode == Postcode
select people).ToList(); Similarly, to do your postcode search do this:var surnameSearch = (from people in PeopleRecordsSearch
where people.Surname.Contains(Surname)
select people).ToList(); This works because the first query is actually an IQueryable, which means that it isn't executed (this is known as deferred execution). The beauty about this is that you can create a base query, and then refine it at a later stage.
|
|
|
|

|
Thanks Pete
that makes sense! its always the simple solutions that you forget
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|

|
Good answer.
Remember that with intermediate IQueryables, you want to stay away from anything that can cause the query to be evaluated. That's mostly obvious things like ToList or iterating over it; I'm not sure if there are any weird gotchas like making grouped or summary queries from one.
|
|
|
|

|
When Pete mentioned it, the reading about deferred execution came flooding back and I was assuming that the solution at the time was going to be some black magic approach.
But questions like this always makes me realise that I should of attempted the simple / basic solutions first!
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|

|
I do simple very well. At least I think that's what the wife means when she describes me as simple.
|
|
|
|

|
Thanks for your help earlier Pete it works like a charm! Now onto my next crisis feature to include
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|