none
How to select all values in a column? RRS feed

  • Question

  • I have tried various techniques like so:

    using(var ctx = new PD5500DBEntities()) { string query = "select TableNames from "+ _GraphSelected; Type type = Type.GetType("PressureVessels.Database." + _GraphSelected); var entities = ctx.Set(type); var curves = entities.SqlQuery(query);

    or

    //var curves = ctx.Database.SqlQuery<TableNamesClass>(query).FirstOrDefault(); //Curves = curves.TableNames; // List<string> Curves; }


    Here is the class:

        class TableNamesClass
        {
           // public string TableNames { get; set; }
    
            public List<string> TableNames { get; set; }
        }

    If I select only one tableName then it'll work but I need all the values in the column as List<string>. 

    Saturday, January 10, 2015 11:17 AM

Answers

  • Hello,

    >> But I want to do the same thing using a Class and get the full column as List<string>.

    Maybe you could consider using system.reflection.emit to construct a class type at runtime and assign their alll column to a List<string>().

    Regards.

    Thursday, January 15, 2015 10:55 AM

All replies

  • Hello,

    >>If I select only one tableName then it'll work but I need all the values in the column as List<string>.

    If I understand correctly, you want to return the selected columns to be stored in the List<T> collection, please have a try with the sample query:

    var result = db.TestTables.Select(t => new List<string>() { t.TestID.ToString(), t.TestName }).ToList();
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 12, 2015 11:24 AM
    Moderator
  • Thanks Fred,

    The thing is that I'll only know the table name at runtime. However, I can get the entity type like so:

                        Type type = Type.GetType("PressureVessels.Database." + _GraphSelected);
                        var entities = ctx.Set(type);


    But then I won't be able to use

    entites.Select()

    I have already solved the problem like so:

                        using (EntityConnection conn =
    new EntityConnection("name=PD5500DBEntities"))
                        {
                            conn.Open();
    
                            string query1 = "select "+ GraphSelected + ".TableNames from PD5500DBEntities." + GraphSelected;
                            using (EntityCommand cmd = new EntityCommand(query1, conn))
                            {
    
                                using (EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                                {
                                    List<string> test = new List<string>();
                                    while (rdr.Read())
                                    {
                                        test.Add(rdr[0].ToString().Trim());
                                      
                                    }
                                      Curves = test;
                                      CurveSelected = _Curves[0];
                                }
                            }
                            conn.Close();
                        }

    But I want to do the same thing using a Class and get the full column as List<string>. 

    Monday, January 12, 2015 11:47 AM
  • Hello,

    >> But I want to do the same thing using a Class and get the full column as List<string>.

    Maybe you could consider using system.reflection.emit to construct a class type at runtime and assign their alll column to a List<string>().

    Regards.

    Thursday, January 15, 2015 10:55 AM