Click here to Skip to main content
13,667,203 members
Rate this:
 
Please Sign up or sign in to vote.
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?

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 13-Jan-14 6:43am
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?
   
No, I simply want to retrieve the data in the selected table.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180820.1 | Last Updated 13 Jan 2014
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100