vb.net - How can I dynamically select my Table at runtime with Dynamic LINQ -
i'm developing application allow engineers conduct simple single table/view queries against our databases selecting database, table, fields.
i how use dynamic linq library sample provide dynamically selecting select, , order clauses @ runtime i'm @ impass on how allot table choice.
is there way provide dynamically selecting "from" table @ run time, , if how provide concrete example or point me in direction of has?
thank ever much.
edit
so both of answers seem saying same general idea. i'm going try convert c# vb , work.
the first answer converts
notinheritable class datacontextextensions private sub new() end sub <system.runtime.compilerservices.extension> _ public shared function gettablebyname(context datacontext, tablename string) itable if context nothing throw new argumentnullexception("context") end if if tablename nothing throw new argumentnullexception("tablename") end if return directcast(context.[gettype]().getproperty(tablename).getvalue(context, nothing), itable) end function end class
but it's tossing me error stating extension methods can defined in modules. when wrap in module tags still gives same error.
so got compile wrapping in module tags , stripping class tags. can pull last line out of , shove directly base method allows execute but, seems coming empty. when try enumerate results there none. not sure if codes problem or new codes issue, i'll test more.
here's conversion of second example, i'm off try see if can them work. i'll questions or results after testing.
'get table type (which corresponds table in context) dim datacontextnamespace = "my.datacontext.namespace" dim type = type.[gettype](datacontextnamespace + tablename) dim table = dc.gettable(type 'add clauses list of them each whereclause string in whereclauses table = table.where(whereclause) next 'generate select clause list of columns dim query = table.[select]([string].format("new({0})"), [string].join(",", selectcolumns))
thanks help. bbl
you can use gettable()
corresponding itable
of data. coupled using dlinq, making relatively easy.
this example uses adventureworks
database. project has context defined in databasetest
assembly in databasetest.adventureworks
namespace.
'' need database , dlinq extensions top imports databasetest.adventureworks imports system.linq.dynamic '' sample inputs dim dc = new adventureworksdatacontext() dim tablename = "contact" dim whereclauses() = {"firstname = ""john"" or lastname = ""smith"""} dim selectcolumns() = {"firstname", "lastname"} '' table type (which corresponds table in database) dim typename = "databasetest.adventureworks." & tablename & ", databasetest" dim entitytype = type.gettype(typename) dim table = dc.gettable(entitytype) dim query iqueryable = table '' add clauses list of them each whereclause string in whereclauses query = query.where(whereclause) next '' generate select clause list of columns query = query.select(string.format("new({0})", string.join(",", selectcolumns)))
in retrospect, using reflection might have been easier way table since have name already. names might not have 1-to-1 correspondence you'll have compensate it.
dim table itable = dc.gettype().getproperty(tablename & "s").getvalue(dc, nothing)
Comments
Post a Comment