jwcoleman87
Well-known member
- Joined
- Oct 4, 2014
- Messages
- 124
- Programming Experience
- Beginner
before, I was doing this:
This worked really well, and still does. Problem is that I found a new way that doesn't involve generating sql commands all day. I can let Linq do that for me, like this:
That one line of code replaced all of this:
And the same can be done for my StatusFilter, EndFilter, SerialFilter, and so on and so forth. That's awesome, really awesome. The problem I'm having is that I'm not sure how to handle combining variables like TechnicianQuery and StatusQuery and EndQuery into one big FilterQuery, which I can rebind to my data grid on the click of a button. I'm going to do some reading throughout tomorrow, but any tips would be greatly appreciated.
Perhaps a join is in line?
Private Sub btnApplyFilter_Click(sender As Object, e As EventArgs) Handles btnApplyFilter.Click Dim TechFilter As String = TechnicianFilter() Dim StatFilter As String = StatusFilter() Dim StartFilter As String = StartTimeFilter() Dim EndFilter As String = EndTimeFilter() Dim SerialFilter As String = SerialNumberFilter() Dim FilterExpression As String = String.Join(" AND ", {TechFilter, StatFilter, StartFilter, EndFilter, SerialFilter}.Where(Function(s) s <> String.Empty)) dgvRepairs.DataSource = RepairBindingSource RepairBindingSource.Filter = FilterExpression End Sub
This worked really well, and still does. Problem is that I found a new way that doesn't involve generating sql commands all day. I can let Linq do that for me, like this:
Dim TechnicianQuery = db.Repairs.Where(Function(t) clbTechnicians.CheckedItems.Contains(t.UserID))
That one line of code replaced all of this:
Private Function TechnicianFilter() As String Dim LogicalOperator As String = " OR " Dim TechFilter As String = "" Dim FilterString As String = "" If clbTechnicians.CheckedItems.Count <= 1 Then LogicalOperator = "" End If Dim index As Integer = 0 For Each itemChecked In clbTechnicians.CheckedItems If index = clbTechnicians.CheckedItems.Count - 1 Then LogicalOperator = "" End If TechFilter = String.Format("TechnicianID = '{0}' {1}", itemChecked.ToString(), LogicalOperator) FilterString += TechFilter index += 1 Next If FilterString <> "" Then FilterString = "(" + FilterString + ")" End If Return FilterString End Function
And the same can be done for my StatusFilter, EndFilter, SerialFilter, and so on and so forth. That's awesome, really awesome. The problem I'm having is that I'm not sure how to handle combining variables like TechnicianQuery and StatusQuery and EndQuery into one big FilterQuery, which I can rebind to my data grid on the click of a button. I'm going to do some reading throughout tomorrow, but any tips would be greatly appreciated.
Perhaps a join is in line?