Detect Datatypes

Posts   
 
    
Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 10-Mar-2009 06:58:26   

vb.net,llblgen 2.6,adapter mode

I am using the below function to build a PredicateExpression based on a string in the form "a,b,c,d"

Works fine but when the datatype is nullable then it fails.

eg data is of type guid eg "355184d1-69cd-4fdd-8887-f87662f03aee,355184d1-69cd-4fdd-8887-f87662f03aad"

oDataType.FullName = "System.Guid" when the datatype is guid but = "System.Nullable`1[[System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] " when it is nullable(of guid)

Is their a better method of detecting the datatype?

Private Function BuildBucket(ByVal oField As IEntityFieldCore, ByVal sIds As String, ByVal oDataType As System.Type) As PredicateExpression Dim arIds As String() Dim i As Int32 Dim bucket As New PredicateExpression If Not sIds Is Nothing AndAlso sIds.Length > 0 Then arIds = sIds.Split(",")

        If oDataType.FullName = "System.Guid" Then
            Dim groupsUIDs As Guid

            For i = 0 To arIds.Length - 1
                groupsUIDs = New Guid(arIds(i).ToString)
                bucket.AddWithOr(New FieldCompareRangePredicate(oField, Nothing, groupsUIDs))
            Next
        End If
        If oDataType.FullName = "System.String" Then
            Dim arStrings As String = ""

            If arIds.Length >= 1 Then
                If arIds.Length <> 1 Then
                    For i = 0 To arIds.Length - 1
                        arStrings = arIds(i).ToString
                        bucket.AddWithOr(New FieldCompareRangePredicate(oField, Nothing, arStrings))
                    Next
                Else
                    arStrings = arIds(0).ToString
                    bucket.AddWithOr(New FieldCompareValuePredicate(oField, Nothing, ComparisonOperator.Equal, arStrings))
                End If
            End If

        End If
        If oDataType.FullName = "System.Int32" Then
            Dim arInt32s As Int32

            For i = 0 To arIds.Length - 1
                arInt32s = arIds(i).ToString
                bucket.AddWithOr(New FieldCompareRangePredicate(oField, Nothing, arInt32s))
            Next
        End If
    End If
    Return bucket
End Function
Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 10-Mar-2009 08:45:29   

How do you call this method?

The ActualDotNetType is stored in the FieldPersistenceInfo class, check the following thread to use similar code to get the ActualDotNetType of a field.

http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=12530

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 10-Mar-2009 11:12:41   

Use the SD.LLBLGen.Pro.ORMSupportClasses.GeneralUtils class' method IsNullableType(Type), to check if a type is a nullable type. So: (I assume you added the proper Imports )


Dim realType As Type = oDataType
If GeneralUtils.IsNullableType(oDataType) Then
    realType = realType.GetGenericArguments()(0)
End If

now your code should use realType.

Frans Bouma | Lead developer LLBLGen Pro