Reputation: 629
I have an application that does a search of our database (exposed via EF) for records meeting certain conditions. We have two main tables (Jobs and Recipients). Recipients are linked to the Jobs table by a Job ID.
The job record has various fields (Process Date, Name, JobType). The recipient has a lot of Name and ID fields (e.g. Account Number, Surname, etc)
I want to present a search screen where they see a list of fields to search on. The ASP.net code then sees which textboxes the user typed in (or selected), and builds a query based on that.
I'm just trying to find the most efficient way possible.
The rough logic is :
if not string.isnullorempty(txtName.text) then
'Query.Where.Add clause = " AND Name like '@txtName'
'Query.WhereParameters.Add("@txtName",txtNAme.text
end if
if not string.isnullorempty(txtAccountNumber.text) then
.....etc
if not string.isnullorempty(txtFromDate.text) then..
Where I'm stuck is figuring out how to add in additional where clauses. Should I just use Entity SQL?
Can this return strongly typed EF objects as well? And is it possible to return both the Jobs and Recipients objects?
e.g. if I did a select recipient., jobs. from....blah would this parse correctly and return linked Recipient and Job objects?
Upvotes: 0
Views: 342
Reputation:
If you start from a strongly-typed unfiltered query, you can add filters using Queryable.Where
.
Name = txtName.Text
If Not String.IsNullOrEmpty(Name) Then
Query = Query.Where(Function(j) j.Name.Contains(Name))
End If
If your unfiltered query correctly links multiple records, those links will be preserved when you add a filter.
(Disclaimer: I don't usually use VB.NET; there may be silly syntax errors, but you should be able to get the general idea.)
Upvotes: 2