Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have an entity framework model in a MVC app.

The user will select a table by table name, and the table data will be retrieved for display in a grid.

I've gotten this far. See the code comments for where I'm stuck. Now that I have the type, how do I do what I need to do?

C#
private void comboTables_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string tableName = comboTables.SelectedItem.ToString();
    try
    {
        using (GLPOC.Data.NGIPEntities nGIP = new GLPOC.Data.NGIPEntities())
        {
            PropertyInfo property = null;
            Type tableType = this.GetTableType(nGIP, tableName, ref property);

            // this is what I'm trying to do in a generic manner - at this point,
            // I have the type and the property that I'm going to use
//          IQueryable<GLMAP_BROAER_DC> tableData = nGIP.GLMAP_BROAER_DC;
//          List<GLMAP_BROAER_DC> result = tableData.AsEnumerable().Select(p => p).ToList();

            // this line retrieves the tables data, but how do I put it into a 
            // list of the property's type
            var temp = property.GetValue(nGIP, null);

            //this.lvData.ItemsSource = (List<myType>)result;
        }
    }
    catch (Exception ex)
    {
        if (ex != null) {}
    }
}

private Type GetTableType(NGIPEntities nGIP, string tableName, ref PropertyInfo info)
{
    // get the properties in the entity
    PropertyInfo[] properties = nGIP.GetType().GetProperties();
    // find the property we want and set the ref parameter
    PropertyInfo property = (from item in properties
                             where item.Name.ToUpper() == tableName
                             select item).FirstOrDefault<PropertyInfo>();
    info = property;
    // determine the property's type
    string propertyName = string.Format("GLPOC.Data.{0}, GLPOC",tableName);
    Type type = Type.GetType(propertyName);
    // return the type
    return type;
}
Posted
Comments
Christopher Drake 13-Jan-14 12:50pm    
Are you trying to add a new list item with that property value as a text value?
#realJSOP 13-Jan-14 14:05pm    
No, I simply want to retrieve the data in the selected table.

1 solution

Try something along the lines of :

string queryString = string.format("select {0} from {1}",fields,tableName); //Paramterise this to stop sql injection
List<ObjectParameter> someList= new List<ObjectParameter>();
///set params
ObjectParameter[] paramList = someList.ToArray();
ObjectQuery<tabletype> dynamicQuery = context.CreateQuery<tabletype>(queryString,paramList)</tabletype></tabletype>


Then you can do like:
var dataItems = from item in dynamicQuery select item;
return dataItems.ToList();


Good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900