I’m working on a project that connects to Oracle. It Brings back data through a dataset. I use Linq to bind it to a collection and throw it back to be read by json. It works great but I can’t help but think – there’s got to be a better way to do this. Here’s an example of what I do. I hope it helps others. Dsp is Dataset.
List<Information> lstSearch = null;
lstSearch = (from l in dsp.Tables[0].AsEnumerable()
select new Information
{
application_id = l["APPLICATION_ID"].ToString(),
hospital_name_1 = l["HOSPITAL_NAME_"].ToString(),
physical_address = l["PHYSICAL_ADDRESS"].ToString(),
// may have to add more here...
}).ToList<Information>();
// serialize and send back as a json string
System.Web.Script.Serialization.JavaScriptSerializer oSerializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
string sJSON = oSerializer.Serialize(lstSearch.First());
Theoretically, yes works. The “information” collection matches the html “name” tag of each control on the page which provides a nice robust binding. My concern lies with having
to go through each field name in order to populate the List<> object.
Isn’t there a specific where clause in where the collection (get/set) property matches the dataset column name thus populating the collection only if the column name (not the value) matches the data row column?
I’d recommend AutoMapper … see this question.
Here’s a very simple example. Make sure that your .NET type exactly mirrors the structure of your DB object or AutoMapper will not work as advertised: