-1

I am used to JavaScript where you can access an object's property using a string like so:

myObject[propertyNameString]

How can I do this for my query of an entity database doing something similar to this:

Dim query = From q In db[tableName]
            Where q[columnName] = myValue
            Select z
6
  • 1
    This is not supported in c# and vb.net. What is the use case here? Commented Jun 15, 2017 at 15:11
  • If you know the name of the property, why not just myObject.propertyName ? Commented Jun 15, 2017 at 15:15
  • @ChetanRanpariya is right. You can kinda sorta do this, but the code will not look similar and be difficult to manage. check this out Commented Jun 15, 2017 at 15:16
  • @Plutonix Because I am using this query inside a for each loop that uses a list of table names with an inner for each loop with a list of column names. I was hoping that I could write these queries dynamically this way. Commented Jun 15, 2017 at 15:16
  • Here's another example where they do this but again, not really similar syntactically Commented Jun 15, 2017 at 15:20

1 Answer 1

1

Here is how I actually went about solving this:

Dim tableName As String = "myTable" 'Set "myTable" dynamically in for loop
Dim tableObjectType As Type = Type.GetType(tableName)
Dim result = dbContext.Set(tableObjectType).Find("myPrimaryKey") 'Also set dynamically
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.