Dowlers
Dowlers

Reputation: 1494

How to use multiple queries on Entity Framework backed datagridview?

I have a form with a datagridview. The datagridview datasource is set to an Entity Framework data context.

_dc = New WarehouseEntities1
_dc.Configuration.ProxyCreationEnabled = False
_dc.USER_MANAGEMENT_INFO.Load()
dgvSUMA.DataSource = _dc.USER_MANAGEMENT_INFO.Local.ToBindingList()

This works fine. I have a number of search boxes that filter the data and these work fine as well.

If txtUserNameSrch.Text <> "" Then
    Dim pOC = _dc.USER_MANAGEMENT_INFO.Local.Where(Function(c) c.USER_NAME.Contains(pQrytxt)).ToList()
    dgvSUMA.DataSource = pOC
End If

If Not chkbxIncludeDropped.Checked Then
    Dim pOC = _dc.USER_MANAGEMENT_INFO.Local.Where(Function(c) c.USER_STATUS.Contains("ACTIVE")).ToList()
    dgvSUMA.DataSource = pOC

End If

Again this works fine except that only one filter is applied at a time. I want to combine the Where queries if they are both set and so far I'm failing. I don't want to combine them in if statements as I have 15 different search controls. Instead I want to apply the filters to the results of the previous filter and then set the datagridview datasource at the end.

This post is similar to what I want to do but in c#

Upvotes: 0

Views: 47

Answers (1)

djv
djv

Reputation: 15774

If you are OK with executing the query up front, then filtering the results, you can do this. Move the declaration of pOC out of each If, so it can be filtered in each place.

Dim pOC = _dc.USER_MANAGEMENT_INFO.Local.ToList() ' database is queried

If txtUserNameSrch.Text <> "" Then
    pOC = pOC.Where(Function(c) c.USER_NAME.Contains(pQrytxt)).ToList() ' filtered
End If

If Not chkbxIncludeDropped.Checked Then
    pOC = pOC.Where(Function(c) c.USER_STATUS.Contains("ACTIVE")).ToList() ' filtered
End If

dgvSUMA.DataSource = pOC

Upvotes: 1

Related Questions