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
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.
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.
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
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
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.
Member
456 Points
350 Posts
How can I assign Linq.Data.Table(of <type>) to a variable, where <type> is a string
Jan 26, 2019 01:47 PM|jzero|LINK
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
All-Star
37051 Points
14951 Posts
Re: How can I assign Linq.Data.Table(of <type>) to a variable, where <type> is a string
Jan 26, 2019 01:54 PM|mgebhard|LINK
Build a dynamic SQL string and execute the SQL using a raw query.
https://docs.microsoft.com/en-us/ef/ef6/querying/raw-sql
Member
456 Points
350 Posts
Re: How can I assign Linq.Data.Table(of <type>) to a variable, where <type> is a string
Jan 26, 2019 02:04 PM|jzero|LINK
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.
All-Star
37051 Points
14951 Posts
Re: How can I assign Linq.Data.Table(of <type>) to a variable, where <type> is a string
Jan 26, 2019 02:21 PM|mgebhard|LINK
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.
Member
456 Points
350 Posts
Re: How can I assign Linq.Data.Table(of <type>) to a variable, where <type> is a string
Jan 26, 2019 03:13 PM|jzero|LINK
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
All-Star
37051 Points
14951 Posts
Re: How can I assign Linq.Data.Table(of <type>) to a variable, where <type> is a string
Jan 26, 2019 03:21 PM|mgebhard|LINK
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.
Member
456 Points
350 Posts
Re: How can I assign Linq.Data.Table(of <type>) to a variable, where <type> is a string
Jan 26, 2019 03:31 PM|jzero|LINK
Thank you, got some ideas.