次のようなクラス(エンティティ)があります。
Public Class Product
Property ID As Integer
Property Name As String
Property IssueDate As Date
Property ExpireDate As Date
Property NextCheckDate As Date
End Class
Entity Framework 5 を使用しており、 where句のいくつかの述語を使用してデータベースにクエリを実行したいと考えています。日付の述語を作成するには、次のような関数を使用します。
Function GetDateIssuePredicate(fromDate As Date?, toDate As Date?) As Expression(Of Func(Of Product, Boolean))
Dim predicate As Expressions.Expression(Of Func(Of Product, Boolean))
If fromDate.hasValue AndAlso toDate.hasValue Then
predicate = (Function(p) p.IssueDate >= fromDate.Value AndAlso p.IssueDate<= toDate.Value)
ElseIf fromDate.hasValue Then
predicate = (Function(p) p.IssueDate >= fromDate.Value)
ElseIf toDate.hasValue Then
predicate = (Function(p) p.IssueDate<= toDate.Value)
End If
Return predicate
End Function
この関数は、特定のエンティティ フィールドIssueDateに対して機能します。異なるエンティティ フィールド (つまり、 ExpireDate、NextCheckDate )に対してまったく同じことを行う関数をさらに作成することは避けたいと思います。これを達成する方法はありますか?