How to implement SQL where in for entity framework 6

entity-framework-6 linq linq-to-entities vb.net where-clause

Question

I am trying to filter a dataset from entity framework table using the below functions. The issue I am experiencing is that when I get to .ToList() I get an object reference error. If I take out the .contains line the query works fine.

Public Function List(entityModel As String, filter As BO.Filter.Import) As IList(Of BO.Import)
    ' Get import data '
    Using context = New RBEntities
        Dim result = context.import_data.Where(Function(s) s.entity_model = entityModel)
        result = ApplyFilter(result, filter)

        Return result.Select(Function(s) New BO.Import(s)).ToList()
    End Using
End Function

Private Function ApplyFilter(result As IOrderedQueryable(Of import_data), filter As BO.Filter.Import) As IOrderedQueryable(Of import_data)
        If Not filter.IDs Is Nothing AndAlso filter.IDs.Count > 0 Then
            If Not filter.IDs(0) = -1 Then
                result = result.Where(Function(w) filter.IDs.Contains(CInt(w.id)))
            End If
        End If

        If Not String.IsNullOrEmpty(filter.Term) Then
            result = result.Where(Function(w) w.data.ToLower().Contains(filter.Term.ToLower()))
        End If

        If Not filter.Status Is Nothing Then
            result = result.Where(Function(w) w.status = filter.Status)
        End If

        If filter.UserProfileID > 0 Then
            result = result.Where(Function(w) w.user_profile_id = filter.UserProfileID)
        End If

        Return result
    End Function
1
0
1/28/2015 11:04:12 PM

Accepted Answer

Managed to resolve the issue by replacing the below

result = result.Where(Function(w) filter.IDs.Contains(CInt(w.id)))

with

result = result.Where(Function(w) filter.IDs.Any(Function(a) a = w.id))
0
1/30/2015 10:03:06 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow