我有一个现有的控制器操作,如下所示。
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
我希望使它更简单,如下所示。
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
如果通过了过滤器,EF 是否会触发两个查询,或者它是否足够智能以触发一个查询?