locked
How can I assign Linq.Data.Table(of <type>) to a variable, where <type> is a string RRS feed

  • Question

  • User-943250815 posted

    Is there a way to a assign a DatabaseContext Table to a variable using variable in place of type?
    I know I can use
    Dim MyTable as Linq.Data.Table(of Products)

    Say if I have 3 tables with different names but all 3 have same structure (fields & types), and would like to make a function to query any table based on function parameter

    Private Function QryTable(byval WhatTable as string) as List(of string)
    Dim MyList as List(of String) = Nothing
    Dim MyDB as new MyDBDatabaseContext
    Dim MyTable as Linq.Data.Table(WhatTable) <--- How can I use function parameter here
    Select Case WhatTable
      Case "table1"
        MyTable = MyDB.ProductsA
      Case "table2"
       MyTable = MyDB.ProductsB
      Case "table3"
       MyTable = MyDB.ProductsC
    Case Else
    Return MyList
    End Select

    MyList = (From p in MyTable select p.Description).ToList
    Return MyList
    End Function

    Saturday, January 26, 2019 1:47 PM

Answers

All replies

  • User475983607 posted

    Build a dynamic SQL string and execute the SQL using a raw query.

    https://docs.microsoft.com/en-us/ef/ef6/querying/raw-sql

    Saturday, January 26, 2019 1:54 PM
  • User-943250815 posted

    Yes I can do it this ways.
    My question is more about conceptual possibilities, if is it possible apply a variable on (of <type>) even with use of another resource.

    Saturday, January 26, 2019 2:04 PM
  • User475983607 posted

    Yes I can do it this ways.
    My question is more about conceptual possibilities, if is it possible apply a variable on (of <type>) even with use of another resource.

    Use reflection to convert the string to a type.   This can be accomplished using Generics, an interface, and where constraint on the Generic method.

    Can you explain the problem?  It seems odd that you know the name of the table but not the type.

    Saturday, January 26, 2019 2:21 PM
  • User-943250815 posted

    mgebhard

    Can you explain the problem? It seems odd that you know the name of the table but Not the type.

    In fact is not a problem, I´m just thinking in a way to use just a single function to do same Linq query on different tables. For sure I know type.
    My first idea was the sample posted, perhaps not proper subject.
    On the other hand, function parameter can be of any type or value, since it give a way to get desired table to be used on Linq query

    Saturday, January 26, 2019 3:13 PM
  • User475983607 posted

    jzero

    In fact is not a problem, I´m just thinking in a way to use just a single function to do same Linq query on different tables. For sure I know type.
    My first idea was the sample posted, perhaps not proper subject.
    On the other hand, function parameter can be of any type or value, since it give a way to get desired table to be used on Linq query

    You're describing the repository pattern.

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

    Be careful.  While the repository patter is a useful pattern it becomes too generic, IMHO, when dealing with complex business logic.   I use the repository pattern for lookup tables or when the UI mirrors a table.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 26, 2019 3:21 PM
  • User-943250815 posted

    Thank you, got some ideas.

    Saturday, January 26, 2019 3:31 PM