none
get objectset instance with name of the table and convert to list RRS feed

  • Question

  • Hi,

    i use vs2010 and write code in c#.

    i derive data using entity framework in my new project.

    i need  to get objectset<> via a string parameter that client choose.  then i will get the data of that table.

    how can i get specific objectset<> using a string and convert the returned object to list ?

    NOTE: actually ,i found out the answer of first part of my problem ,please check the code below

    string table_name="TABLE1";

    var os =  ent.GetType().GetProperty(table_name).GetValue(ent, null) ;

    but i dont know convert it to list so i can look in cells of table

    Thank you already

    ISMAIL

    • Moved by Mike FengModerator Tuesday, September 11, 2012 5:52 AM EF (From:.NET Base Class Library)
    Monday, September 10, 2012 11:22 AM

Answers

  • Hi,

    One question, if we don't know what the generic type it is (ObjectSet<?>), how can you use LINQ during design time then?   We can of course use .NET Reflection as you mentioned to get the query value, and also we can use the dynamic function of C# 4.0 to get the object value directly, e.g.

                    dynamic query = context.GetType().GetProperty("Person").GetValue(context, null);

                    foreach (dynamic q in query)
                    {
                        Console.WriteLine(q.ID);
                        Console.WriteLine(q.Name);
                    }

    However, when talking about LINQ query, we need to know what the type it is during design time.  Otherwise, you will need something like dynamic EF queries.  Here are some references which can help you start:

    http://www.albahari.com/nutshell/predicatebuilder.aspx

    http://geekswithblogs.net/DavidBarrett/archive/2012/02/06/using-entity-framework-to-query-dynamic-types.aspx

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us


    Friday, September 14, 2012 6:23 AM
    Moderator

All replies

  • Hi Karynch,

    Welcome to the MSDN Forum.

    I have moved this thread to EF forum for better support.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, September 11, 2012 5:51 AM
    Moderator
  • Hi,

    i use vs2010 and write code in c#.

    i derive data using entity framework in my new project.

    i need  to get objectset<> via a string parameter that client choose.  then i will get the data of that table.

    how can i get specific objectset<> using a string and convert the returned object to list ?

    NOTE: actually ,i found out the answer of first part of my problem ,please check the code below

    string table_name="TABLE1";

    var os =  ent.GetType().GetProperty(table_name).GetValue(ent, null) ;

    but i dont know convert it to list so i can look in cells of table

    Thank you already

    ISMAIL

    • Merged by Alexander Sun Wednesday, September 12, 2012 7:49 AM Duplicate
    Tuesday, September 11, 2012 10:02 AM
  • thank you,

    i could not find my question at:

    http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/threads/

    so i created it.

    ISMAIL

    Tuesday, September 11, 2012 10:03 AM
  • Hi karynch;

    The below code snippet will do what you need. The code came from an article written by Rowan Miller of Microsoft Called What Tables Are In My EF Model And My Database.

    // Holds the list of table names
    List<string> tableNames = new List<string>();
    
    using( var db = new NorthwindWithContactsEntities( ) )
    {
        // If you are using Model/Database First and haven’t used the context to access 
        // any data in the current AppDomain you may get an InvalidOperationException 
        // stating ‘”The space ‘SSpace’ has no associated collection.”. This is because 
        // metadata is loaded only when it is needed. The easiest way to fix this is to 
        // run a quick query before trying to access the metadata.
        var temp = db.[Any Database Table In Your Model].Count( );
    
        // If you are using Object Context use the following statement
        var metadata = db.MetadataWorkspace;
        
        // If you are using DbContext comment out the above statement and un-comment
        // the next statement
        //var metadata = ( ( IObjectContextAdapter ) db ).ObjectContext.MetadataWorkspace;
    
        var tables = metadata.GetItemCollection( DataSpace.SSpace )
          .GetItems<EntityContainer>( )
          .Single( )
          .BaseEntitySets
          .OfType<EntitySet>( )
          .Where( s => !s.MetadataProperties.Contains( "Type" )
            || s.MetadataProperties[ "Type" ].ToString( ) == "Tables" );
    
        foreach( var table in tables )
        {
            var tableName = table.MetadataProperties.Contains( "Table" )
                && table.MetadataProperties[ "Table" ].Value != null
              ? table.MetadataProperties[ "Table" ].Value.ToString( )
              : table.Name;
    
            tableNames.Add(tableName);
        }
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, September 11, 2012 2:04 PM
  • Thanks for your quick reply,

    actually i need to reach the data in the table- not the name of it  and also i need  table as a objectset<> so i can query it via Linq.

    Is it possible?

    Wednesday, September 12, 2012 6:14 AM
  • Hi,

    One question, if we don't know what the generic type it is (ObjectSet<?>), how can you use LINQ during design time then?   We can of course use .NET Reflection as you mentioned to get the query value, and also we can use the dynamic function of C# 4.0 to get the object value directly, e.g.

                    dynamic query = context.GetType().GetProperty("Person").GetValue(context, null);

                    foreach (dynamic q in query)
                    {
                        Console.WriteLine(q.ID);
                        Console.WriteLine(q.Name);
                    }

    However, when talking about LINQ query, we need to know what the type it is during design time.  Otherwise, you will need something like dynamic EF queries.  Here are some references which can help you start:

    http://www.albahari.com/nutshell/predicatebuilder.aspx

    http://geekswithblogs.net/DavidBarrett/archive/2012/02/06/using-entity-framework-to-query-dynamic-types.aspx

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us


    Friday, September 14, 2012 6:23 AM
    Moderator
  • thank you
    Friday, September 14, 2012 6:35 AM
  • You're welcome.

    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us

    Friday, September 14, 2012 6:40 AM
    Moderator