To work around and get this functionality, there is a workaround. This workaround involves our time tested reflection technique. The vb code for this is as follows:
Dim myConnectionString as string = "" 'assign connection string here
Dim columnName as string = "" 'assign column name here
Using db as new myDataContext(myConnectionString)
Dim myValues = (From customer in Customers Select customer.GetType().GetProperty(columnName).GetValue(customer, nothing))
End Using
Here we have used GetProperty() method to get the PropertyInfo object. PropertyInfo is defined in System.Reflection namespace.
Though we are getting this great flexibility but this flexibility is coming with a cost. If you use SQL debug visualizer to look at the SQL resulting with the above LINQ query, you will realize that the resulting SQL select all column from the database instead of just that column specified in GetProperty() method. This is an additional cost other than the runtime cost associated with reflection.
No comments:
Post a Comment