Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
SQL
I want a LINQ query to work with Entity Framework to List All the Tables in Datamodel.
i.e. Same as "Select * from SysObjects where xtype='U' " in SQL Server
or "Select * from tab" in Oracle.
Posted
Updated 30-May-17 8:40am

Hi,

I got a solution about this, but I found it less helpful in real time because it is not dynamic, I got another solution using Reflection.

In this I m finding all types of currently executing Assembly so, I have to manually remove Program and TestDBEntities.


C#
using System;
using System.Reflection;

namespace RetrievingORMTablesUsingReflection
{
    class Program
    {
        TestDBEntities DbOrm = new TestDBEntities();
        static void Main(string[] args)
        {
            Assembly A = Assembly.Load(Assembly.GetExecutingAssembly().FullName);
            Type[] Types = A.GetTypes();
            foreach (Type T in Types)
            {
                if (T.Name != "Program" && T.Name != "TestDBEntities")
                    Console.WriteLine(T.Name);
            }
            Console.Read();
        }
    }
}
 
Share this answer
 
The probably best way to list out entities is probably like this:

C#
private void PopulateTableNames()
{
    List<string> listOfTables= new List<string>();
    tabeller.Clear();
    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");
    var PropertiesInTables = tables.Select(s => s.ElementType.Properties).ToList();
    foreach (var table in tables)
    {
        var tableName = table.MetadataProperties.Contains("Table")
            && table.MetadataProperties["Table"].Value != null
            ? table.MetadataProperties["Table"].Value.ToString()
            : table.Name;
        var tableSchema = table.MetadataProperties["Schema"].Value.ToString();
        listOfTables.Add(tableName);
    }
}


Attention should also go to this line in the code sample above:
C#
var PropertiesInTables = tables.Select(s => s.ElementType.Properties).ToList();
This Linq query also gives you the fields (properties) of each table.

[From romiller.com]
 
Share this answer
 
v3
Comments
Dave Kreskowiak 30-May-17 14:42pm    
Asked and answered FOUR YEARS AGO.
netfed 30-May-17 23:34pm    
Well, there you go :-) And to all the others who are reading this; it's a fine update
CHill60 1-Jun-17 18:34pm    
" it's a fine update" ... in your opinion. The thing is this is a Question and Answer forum, not a reference.
[no name] 31-May-17 15:59pm    
Member since about 11 years, title Student. What you expect other :-) ?
netfed 31-May-17 23:32pm    
Aha! Didn't see that. I have updated my profile. But stil, it is quite possible I become a student once more in my lifetime :-)
Check this link, there is an easy way:
Linq to Sql, sys tables
 
Share this answer
 
Hi

Please follow of links :

http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
Understanding LINQ (C#)[^]
http://www.codeproject.com/Articles/628671/LINQ-to-Family-Tree-Prolog-Style

Best Regards.
 
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