F# LINQ type class fit in one -



F# LINQ type class fit in one -

i learning linq f# 3.0. want know how convert old class utilize new linq features in f# 3.0 example, have created simple info table in sql server 2008 r2, database name mydatabase.

-- create table1 table. create table [dbo].[table1] ( [id] int not null, [testdata1] int not null, [testdata2] float (53) not null, [name] ntext not null, primary key clustered ([id] asc) ); utilize mydatabase insert table1 (id, testdata1, testdata2, name) values(1, 10, 5.5, 'testing1'); insert table1 (id, testdata1, testdata2, name) values(2, 20, -1.2, 'testing2');

for f# 2.0, can create little class retrieve info table1 , homecoming main program.

#light module mydata open scheme open system.collections.generic open system.data open system.data.sqlclient type getdata(tablename: string) = class allow sqlconn = "server=(local); integrated security=true; database=mydatabase" allow conndb = new sqlconnection(sqlconn) allow sqlbyfun (cmdtext, unwrapping) = utilize cmd = new sqlcommand(cmdtext, conndb) allow reader = cmd.executereader() seq { while reader.read() yield unwrapping(reader) } allow dbtable1(tablename: string) = allow sql = "select id, testdata1, testdata2, name table1 order id" conndb.open() allow info = sqlbyfun(sqlchanges, (fun reader -> (reader.getint32(0), reader.getint32(1), reader.getdecimal(2),reader.getstring(3)))) |> seq.toarray conndb.close() info fellow member this.gettable(table1) = dbtable1(table1)

then in main program: program.fs, can utilize class of mydata:

#light open mydata open scheme open system.collections.generic open system.data open system.data.sqlclient allow db = new mydata.getdata("table1") allow table1data = db.gettable("table1") printfn "done"

now, f# 3.0 , linq, can re-write class this:

#light open scheme open system.data open system.data.sqlclient open system.data.linq open microsoft.fsharp.data.typeproviders open microsoft.fsharp.linq [<generate>] type dbschema = sqldataconnection<"data source=.;initial catalog=mydatabase;integrated security=true"> allow db = dbschema.getdatacontext() allow dbtable1(tablename: string) = allow info = query { row in db.table1 sortby row.id select (row.id, row.testdata1, row.testdata2, row.name) } info

i can see part, code much shorter , cleaner, want in class , can called main program. how can set above linq code class , used main program? thanks, john

it not exclusively clear question you're trying achieve. in samples, new class (and method) take tablename parameter, you're reading info table1 anyway.

writing linq code parameterized table name not going possible, because different tables may have different columns , cannot utilize same type access them. if have different tables same structure, can merge them single table , add together additional column distinguish between records. if want access different tables, you'll need write different query every table.

regarding encapsulation in class, can in f# 3.0:

// global declarations sql type provider [<literal>] allow connstr = "data source=.;initial catalog=mydatabase;integrated security=true" [<generate>] type dbschema = sqldataconnection<connstr> // declaration of class exposes info access functionality type dataaccess() = // note: can pass different connection string 'getdatacontext' // if want maintain connection string in config file instead of in code. allow db = dbschema.getdatacontext() /// reads info table1 fellow member x.loadtable1() = query { row in db.table1 sortby row.id select (row.id, row.testdata1, row.testdata2, row.name) }

linq linq-to-sql f#

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -