none
Getting ObjectSet based on a String value. RRS feed

  • Question

  • I am new to entity framework am struggling to come up with a solution.

    I have a ObjectContext and I have a string value and want to get a ObjectSet which contains an object with the same name as the string value.

    I can get the objectset through reflection

    ObjectContext.GetProperty("objectName").GetValue(objectContext, null); 

    But I want to get the ObjectSet<objectName> so I can bind objectset to gridview, I also need incode to generate the columns of the gridview based on the objectName colums.


    Simon
    Friday, August 12, 2011 12:57 AM

Answers

  • Hi Simon,

    Welcome!

    According to your description, I'm not clear about your question.

    As you said, we can use reflection to get the ObjectSet:

    using (var context = new EFTestEntities())
          {
            var test =context.GetType().GetProperty("T_Employee").GetValue(context, null);
          }
    


    The return type is ObjectSet<T_Employee>.

    Please feel free to let me know your concern, thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 12, 2011 9:10 AM
    Moderator

All replies

  • Hi Simon,

    Welcome!

    According to your description, I'm not clear about your question.

    As you said, we can use reflection to get the ObjectSet:

    using (var context = new EFTestEntities())
          {
            var test =context.GetType().GetProperty("T_Employee").GetValue(context, null);
          }
    


    The return type is ObjectSet<T_Employee>.

    Please feel free to let me know your concern, thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 12, 2011 9:10 AM
    Moderator
  • Hi,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 17, 2011 1:08 PM
    Moderator
  • Sorry been out of action for a few weeks. Will look at your solution and get back.

     

    Thanks.


    Simon
    Sunday, September 4, 2011 11:45 PM
  • I guess my question is with respect to the answer you have is how to use reflection further to get Object data from test.

    As test is in the format ObjectSet<Employee> how do i iterate through the objectset and get each Employee object.

    Hope that makes sense, sorry I didnt reply earlier but had an operation and been out of action for several weeks.

    Thanks for the response.


    Simon
    Monday, September 5, 2011 5:26 AM
  • Hi Simon,

    Thanks for your feedback.

    If you want to bind to GridView, you just need to set its datasource, I'll my code here:

    EF4.0:

     public partial class _Default : System.Web.UI.Page
        {
            EFTestEntities context = new EFTestEntities();
            protected void Page_Load(object sender, EventArgs e)
            {
                var test = context.GetType().GetProperty("T_Employee").GetValue(context, null);
                ((ObjectSet<T_Employee>)test).ToList();
                this.GridView1.DataSource = ((ObjectSet<T_Employee>)test);
                this.GridView1.DataBind();
            }
        }
    

    EF4.1

       public partial class _Default : System.Web.UI.Page
        {
            EFTestContext context = new EFTestContext();
            protected void Page_Load(object sender, EventArgs e)
            {
             var test = context.GetType().GetProperty("T_Employee").GetValue(context, null);
                ((DbSet<T_Employee>)test).Load();
                this.GridView1.DataSource = ((DbSet<T_Employee>)test).Local;
             this.GridView1.DataBind();
            }
        }
    


    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, September 6, 2011 9:58 AM
    Moderator
  • Thanks for the reply, i do wish to bind to a grid view however the reason I use a string value to get the object set is that I have many reference data tables which I want to dynamically build the gridview columns and then bind.

    So to expand on your example lets say the user selects from a drop down list one of the follwoing tables "T_Employee", "T_Tree","T_Car" and "T_Fish" all tables with varying fields. In your example we can get the ObjectSet as a variant by using

    string tableName = "T_Car";

    var test = context.GetType().GetProperty(tableName).GetValue(context, null);

    now as the obect in the object set varies I cant

     ((ObjectSet<tableName>)test).ToList();

    to bind to a grid view, I also need to do something like

    tableNameObject.GetProperties() to get the table fields to dynamically build the grid view columns.

    Ultimately I am building one windows form which is used to administer all reference tables of which I have many.

     


    Simon
    Tuesday, September 6, 2011 11:45 PM