none
Is it possible to get the table by knowing its name only (string) RRS feed

  • Question

  •         public bool IsShowTable
            {
                get { return _IsShowTable; }
                set { _IsShowTable = value;
                if (_IsShowTable)
                {
                    //Type type = Type.GetType(GraphSelected);
                    //Object o = (Activator.CreateInstance(type));
                    using (var ctx = new PD5500DBEntities())
                    {               
                        DataGridSrc = new ObservableCollection<object>(ctx.G2_2_6_0dot01_0dot0); //Using here the TableName in String
                    }
                }
                NotifyPropertyChanged();
                }
            }
    
            private ObservableCollection<Object> _DataGridSrc;
    
            public ObservableCollection<Object> DataGridSrc
            {
                get { return _DataGridSrc; }
                set { _DataGridSrc = value;
                NotifyPropertyChanged();
                }
            }
    

    In the code I used ctx.TableName, would it be possible to get a reference by knowing the string of the TableName only. 

    I mean

    TableName = "MyTable";

    //Type type = Type.GetType("TableName");
    //Object o = (Activator.CreateInstance(type));


    Monday, January 5, 2015 1:46 PM

Answers

  • You could use the DbContext.Set method:

            Type type = Type.GetType("YourNamespace.YourEntityType");
            var entities = context.Set(type);
    

    Please remember to mark helpful posts as answer and/or helpful and please remember that a new question deserves a new thread.
    Monday, January 5, 2015 2:03 PM

All replies

  • You could use the DbContext.Set method:

            Type type = Type.GetType("YourNamespace.YourEntityType");
            var entities = context.Set(type);
    

    Please remember to mark helpful posts as answer and/or helpful and please remember that a new question deserves a new thread.
    Monday, January 5, 2015 2:03 PM
  • Thanks Magnus.

    But now I have difficulty setting it to datagrid source 

            //public ObservableCollection<Object> DataGridSrc
            //{
            //    get { return _DataGridSrc; }
            //    set
            //    {
            //        _DataGridSrc = value;
            //        NotifyPropertyChanged();
            //    }
            //}
    
    or
    
            private ICollectionView _DataGridSrc;
    
            public ICollectionView DataGridSrc
            {
                get { return _DataGridSrc; }
                set
                {
                    _DataGridSrc = value;
                    NotifyPropertyChanged();
                }
            }


                    using (var ctx = new PD5500DBEntities())
                    {
                        Type type = Type.GetType("PressureVessels.Database.G2_2_6_0dot01_0dot0");
                        var entities =  ctx.Set(type);
                        DataGridSrc = (ICollectionView)entities.Local;
                       //or
                        //DataGridSrc = new ObservableCollection<object>(entities.Local);
                    }

    Before when I was using 

     DataGridSrc = new ObservableCollection<object>(ctx.G2_2_6_0dot01_0dot0);

    it worked but now it doesn't. Though now by using the namespace.name I get the type but still cannot assign it to datagridSr which is ObservableCollection or ICollectionView. 


    Monday, January 5, 2015 3:18 PM
  • The Local property of the non-generic DbClass returns an IList and you cannot cast an IList to a ICollectionView.

    You should be able to cast it to a IEnumerable<object> though:


    using (var ctx = new PD5500DBEntities())
                    {
                        Type type = Type.GetType("PressureVessels.Database.G2_2_6_0dot01_0dot0");
                        var entities =  ctx.Set(type);
                        DataGridSrc = new ObservableCollection<object>((IEnumerable<object>)entities.Local);
                    }
    Please remember to mark all helpful posts as answer.
    Monday, January 5, 2015 3:40 PM