0

次のようなクラス(エンティティ)があります。

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に対して機能します。異なるエンティティ フィールド (つまり、 ExpireDateNextCheckDate )に対してまったく同じことを行う関数をさらに作成することは避けたいと思います。これを達成する方法はありますか?

4

1 に答える 1

0

Product の関数ではなく、DateTime の関数にすることもできます。

Function GetDateIssuePredicate(fromDate As Date?, toDate As Date?) As Expression(Of Func(Of DateTime, DateTime)) 

    Dim predicate As Expressions.Expression(Of Func(Of DateTime, DateTime)) 

    If fromDate.hasValue AndAlso toDate.hasValue Then 
        predicate = (Function(d) d >= fromDate.Value AndAlso d <= toDate.Value) 
    ElseIf fromDate.hasValue Then 
        predicate = (Function(d) d >= fromDate.Value) 
    ElseIf toDate.hasValue Then 
        predicate = (Function(d) d <= toDate.Value) 
    End If 

    Return predicate 

End Function 
于 2012-10-09T16:42:43.023 に答える