0

以下のような既存のコントローラーアクションがあります。

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

Filter が渡された場合、EF は 2 つのクエリを起動しますか?それとも 1 つのクエリだけを起動するのに十分なほどインテリジェントですか?

4

2 に答える 2

1

試してみてください!

SQL Management Studio (または類似のもの) を起動し、SQL プロファイラーをロードして、DB に接続し、受信クエリを確認します。

理論的には、WHERE 句が 2 回目に適用されるまでデータの要求が来ないため、EF4 が単一の操作として実行できない理由はありません。

  • クエリの作成
  • where句を追加
  • 2 番目の where 句を追加
  • データ取得の実行
于 2012-07-27T10:04:11.673 に答える
0

はい、1つのクエリ-これはIQueryableインターフェイスの性質です。IQueryableを返すすべてのメソッドは、IQueryableの列挙子にアクセスするまで何も実行しないでください。

于 2012-07-27T10:08:02.300 に答える