This is a migrated thread and some comments may be shown as answers.

FieldValue LINQ query returns the same result

1 Answer 76 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mark
Top achievements
Rank 1
Mark asked on 24 Mar 2011, 04:27 PM
Hello,

I am trying to use a LINQ query to get the value of a particular field from a database view (VwUserInformation). I pass in the name of the field that I want (fieldName) into the function and use it in the query as the parameter for FieldValue. See the code below.

On the first call to the function I set fieldName to "UserID", I get the data that is in the UserID field. On the second call, fieldName equals "UserFullName", but I still get the data from the UserID field. Even if I put in a bad value for fieldName (a field name that doesn't exist in the table), I still get the UserID and no error. Is there a way to fix this so I get the value of the field I specify and not just the value of the first field that was sent to this function?

Mark

    Public Function GetUserData(ByVal fieldName As StringByVal userID As StringOptional ByVal defaultValue As String = ""As String Implements IMedVizerDbCom.GetUserData
        Dim dbData As String
        Dim tempUserData As Object
        Dim id As New Guid(userID)
 
        dbData = Nothing
 
        Dim x As IQueryable(Of Object) = From users In dbContext.VwUserInformation
                                         Where (users.UserID = id)
                                         Select users.FieldValue(Of Object)(fieldName)
 
        tempUserData = x.First
 
        If (Object.ReferenceEquals(tempUserData.GetType(), New Boolean().GetType)) Then
            If (DirectCast(tempUserData, Boolean) = TrueThen
                dbData = "1"
            Else
                dbData = "0"
            End If
        Else
            dbData = tempUserData.ToString
        End If
 
        If (dbData Is NothingThen
            dbData = defaultValue
        End If
 
        Return dbData
    End Function

1 Answer, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 29 Mar 2011, 04:19 PM
Hello Mark,

I am afraid that there is an issue with the FieldValue method when it is used in a function. We will investigate the cause for this issue and fix it.
Actually there is a workaround that you could use in order to avoid the error. I prepared a sample application that shows how to achieve that goal. It uses a dynamic LINQ in order to select the correct data.
I am sorry for the inconvenience caused.
You could find your Telerik points updated.

All the best,
Damyan Bogoev
the Telerik team
Tags
Getting Started
Asked by
Mark
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
Share this question
or