Linq
-
Hi, Am writing a function which should return a dataset to bind to a grid control. It is a linq query, Can I type cast var type variable to DataSet and return it. Here is my funtion below. public DataSet GetSites() { SiteTerDBDataContext ctx = new SiteTerDBDataContext(); ctx.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["DB_ConnectionString"].ConnectionString; var res = from sites in ctx.tblSites select sites; return (DataSet)res; } Any early replies will be appreciated. regards Sajid
-
Hi, Am writing a function which should return a dataset to bind to a grid control. It is a linq query, Can I type cast var type variable to DataSet and return it. Here is my funtion below. public DataSet GetSites() { SiteTerDBDataContext ctx = new SiteTerDBDataContext(); ctx.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["DB_ConnectionString"].ConnectionString; var res = from sites in ctx.tblSites select sites; return (DataSet)res; } Any early replies will be appreciated. regards Sajid
Sayed Sajid wrote:
Any early replies will be appreciated.
You're in a hurry, but you didn't think to try this and see what happened, or check res in the debugger to see what type it is ? I would be stunned if it came back as a dataset. I expect it would be a collection.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Hi, Am writing a function which should return a dataset to bind to a grid control. It is a linq query, Can I type cast var type variable to DataSet and return it. Here is my funtion below. public DataSet GetSites() { SiteTerDBDataContext ctx = new SiteTerDBDataContext(); ctx.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["DB_ConnectionString"].ConnectionString; var res = from sites in ctx.tblSites select sites; return (DataSet)res; } Any early replies will be appreciated. regards Sajid
Sayed Sajid wrote:
Can I type cast var type variable to DataSet and return it.
No.
var
is just a syntactic sugar. In your case,res
will be enumerable ofctx.tblSites
's type. You can't cast it directly to aDataSet
. Create aDatTable
, Iterate the contents ofres
in a loop and fill data intoDataTable
. Once data table is ready, add it to aDataSet
and return. :)Navaneeth How to use google | Ask smart questions
-
Sayed Sajid wrote:
Any early replies will be appreciated.
You're in a hurry, but you didn't think to try this and see what happened, or check res in the debugger to see what type it is ? I would be stunned if it came back as a dataset. I expect it would be a collection.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
Mr Chris, Thanks for your answer. What would be the appropiate way of returning var type? This is my intension to do actually. public var GetSites() { SiteTerDBDataContext ctx = new SiteTerDBDataContext(); ctx.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["DB_ConnectionString"].ConnectionString; var res = from sites in ctx.tblSites select sites; return res; } Many thanks. Sajid
-
Mr Chris, Thanks for your answer. What would be the appropiate way of returning var type? This is my intension to do actually. public var GetSites() { SiteTerDBDataContext ctx = new SiteTerDBDataContext(); ctx.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["DB_ConnectionString"].ConnectionString; var res = from sites in ctx.tblSites select sites; return res; } Many thanks. Sajid
You cannot return var type since it's not a type like int or string. It simply allows the compiler to infer the real type.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis