I have an existing controller action as below.
Public Function List(ByVal UserID As Integer, Optional ByVa; Filter As String = Nothing) As ActionResult
Dim records
If Filter IsNot Nothing Then
records = context.Contacts.Where(Function(x) x.UserID = UserID and x.Name.Contains(Filter))
Else
records = context.Contacts.Where(Function(x) x.UserID = UserID)
End If
return View(records)
End Function
I desire to make it simpler as below.
Public Function List(ByVal UserID As Integer, Optional ByVa; Filter As String = Nothing) As ActionResult
Dim records = context.Contacts.Where(Function(x) x.UserID = UserID)
If Filter IsNot Nothing Then
records = records.Where(Function(x) x.Name.Contains(Filter))
End If
return View(records)
End Function
Will EF fire two queries if Filter is passed or is it intelligent enough to fire just one query?
Yes, one query – this is the nature of the IQueryable interface. All methods which return an IQueryable should not execute anything until the IQueryable’s enumerator is accessed.