none
Get table name for type RRS feed

  • Question

  • All I want to do is get the table name for a particular dbml object type.

     

    <Table(Name:=

    "dbo.LookupRenumerationBasis")> _

    Partial

     

    Public Class LookupRenumerationBasi

    So I want the table name dbo.LookupRenumerationBasis.

    I've tried this but can't seem to locate the name of the table.

     

     

    Public Function GetTable(ByVal type As Type) As ITable

     

    Dim returnValue As ITable

    returnValue = _context.GetTable(type)

     

    Return returnValue

     

    End Function


    "I have no particular talent. I am merely inquisitive." -- Albert Einstein
    Wednesday, November 17, 2010 5:00 PM

Answers

  • Hi,

    After the .dbml is created, by default, the table names and the data class names are matched. So you can get the data class type by the table name by .NET Reflection.  Please refer to the following codes:

    ================================================================
                MyDataContext db = new MyDataContext();
                Assembly assembly = Assembly.GetExecutingAssembly();

                Type t = assembly.GetType("Namespace." + strTableName);

                if (t != null)

                {

                    var foos = db.GetTable(t);

     

                    foreach (var f in foos)

                    {

                        PropertyInfo pi = f.GetType().GetProperty("Foo");

                        int value = (int)pi.GetValue(f, null);

                        Console.WriteLine(value);

                    }

                }

    ================================================================

    Hope this can help you,

    JAReyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solucion de esta pregunta te ha sido útil.
    • Marked as answer by Ģ®€ğ§QĻ Thursday, November 18, 2010 2:57 PM
    Wednesday, November 17, 2010 6:44 PM
  • myDataContext.Mapping.GetTable(typeof(type)).TableName

    [)amien

     

    • Marked as answer by Ģ®€ğ§QĻ Thursday, November 18, 2010 2:57 PM
    Wednesday, November 17, 2010 9:15 PM
    Moderator

All replies

  • Hi,

    After the .dbml is created, by default, the table names and the data class names are matched. So you can get the data class type by the table name by .NET Reflection.  Please refer to the following codes:

    ================================================================
                MyDataContext db = new MyDataContext();
                Assembly assembly = Assembly.GetExecutingAssembly();

                Type t = assembly.GetType("Namespace." + strTableName);

                if (t != null)

                {

                    var foos = db.GetTable(t);

     

                    foreach (var f in foos)

                    {

                        PropertyInfo pi = f.GetType().GetProperty("Foo");

                        int value = (int)pi.GetValue(f, null);

                        Console.WriteLine(value);

                    }

                }

    ================================================================

    Hope this can help you,

    JAReyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solucion de esta pregunta te ha sido útil.
    • Marked as answer by Ģ®€ğ§QĻ Thursday, November 18, 2010 2:57 PM
    Wednesday, November 17, 2010 6:44 PM
  • My class name is not the same as the table name. I want the table name LookupRenumerationBasis not the class name LookupRenumerationBasi

    <Table(Name:=

    "dbo.LookupRenumerationBasis"

    Partial

     

    Public Class LookupRenumerationBasi

    I think your code gives the class name.

     


    "I have no particular talent. I am merely inquisitive." -- Albert Einstein
    )> _
    Wednesday, November 17, 2010 8:22 PM
  • myDataContext.Mapping.GetTable(typeof(type)).TableName

    [)amien

     

    • Marked as answer by Ģ®€ğ§QĻ Thursday, November 18, 2010 2:57 PM
    Wednesday, November 17, 2010 9:15 PM
    Moderator
  • Yes Damien,

    That's what my sample code try to shows, besides explain how to access properties by name too.

    JAReyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solucion de esta pregunta te ha sido útil.
    Wednesday, November 17, 2010 10:19 PM
  • Many thanks to both of you.
    "I have no particular talent. I am merely inquisitive." -- Albert Einstein
    Thursday, November 18, 2010 2:57 PM