locked
How to enumerate the properties of an entity

    Question

  • I need to enumerate the property names from a Lightswitch entity.
    The following code can enumerate the entity names in my DataWorkspace, but I don't know how to get the field names within each Entity.
     
     
          For Each ent In DataWorkspace.ApplicationData.Details.Properties.All
            For Each prp In ??? WHAT COLLECTION DO I PUT HERE ???
              System.Diagnostics.Debug.WriteLine(ent.Name & ": " & prp.Name)
            Next
          Next
    
    

    Jim
    Friday, April 15, 2011 11:53 AM

Answers

  • What you're enumerating in your outer loop is actually instances of entity sets, not entities.  Entity sets are the containers of entities.  Depending on what you're actually trying to accomplish, there are a few ways you could do things.

    If you need to enumerate the properties of an entity type but are not guaranteed to actually have an instance of an entity within an entity set, you can use the entity's model to enumerate the properties rather than the entity instance.

                Dim entitySetProperties = From prop In Me.DataWorkspace.ApplicationData.Details.Properties.All
                                          Where TypeOf prop Is IDataServiceEntitySetProperty
                                          Select prop

                For Each entitySetProperty In entitySetProperties
                    Dim entitySetModel = entitySetProperty.GetModel
                    Dim entityTypeModel = DirectCast(entitySetModel, IEntitySetDefinition)
                    For Each entityProperty In entityTypeModel.EntityType.Properties

                    Next
                Next

    If you've got the entity instance, you can do this:

                For Each prop In entity.Details.Properties.All
                Next

    Friday, April 15, 2011 12:59 PM