I am new to MVC. I am however not new to C# and ASP.NET. I am tring to creeate an Web API service that does a simple query to my database (we use Oracle) to a single table. How to program nmy ApiController class to access the database, load the datatable
then loop through the datatble to create a string[]. For example I have the below working but I want the values for the employee data to come from my datatable:
Employee[]
employee = newEmployee[] {
new Employee{Employee_Id=1,Employee_Name="Richa",Employee_Address="Noida",Employee_Contact=12567,Employee_Salary=5000}
Doesn't look like your current method is returning a string array. If you want Employee array then read on.
One idea would be to use the oracle connector and entity framework. You add an entity framework to your model of your MVC. Then you could easily do something like this with linq:
using(var context = new OracleEntities())
{
var employees = context.Employees.ToArray();
}
Of course you could keep the EF seperate from model objects you already have. So if you already have an Employee class you could also do something like this instead of the above:
var employees = context.Employees.Select(s => new Employee(s.Employee_Id, s.Employee_Name, etc).ToArray();
Last, I just realized if you already have a DataTable, you can still use linq. Just do dt.AsEnumerable().ToArray(); That would product an array of DataRows, so what you would do to get an array of Employee objects is the same select example above. dt.AsEnumerable.Select(s
=> new Employee( etc etc)).ToArray().
Thanks for the reply. I was able to figure out by using dt.AsEnumerable().ToArray(). Thanks for all the other info. I'll look into using the Entity Framework for Oracle. This MVC, RESTful Services, XAML etc is all very new, and I might add, overwhelming
for me coming from an ASP.NET programming background and creating websites. Thank goodness for forums like this.
jehrenfeld
0 Points
3 Posts
converting datable to a string[]
Nov 26, 2012 08:12 PM|LINK
I am new to MVC. I am however not new to C# and ASP.NET. I am tring to creeate an Web API service that does a simple query to my database (we use Oracle) to a single table. How to program nmy ApiController class to access the database, load the datatable then loop through the datatble to create a string[]. For example I have the below working but I want the values for the employee data to come from my datatable:
Employee[] employee = new Employee[]
{
new Employee{Employee_Id=1,Employee_Name="Richa",Employee_Address="Noida",Employee_Contact=12567,Employee_Salary=5000}
};
aspwhiz
Member
363 Points
102 Posts
Re: converting datable to a string[]
Nov 27, 2012 06:04 PM|LINK
Doesn't look like your current method is returning a string array. If you want Employee array then read on.
One idea would be to use the oracle connector and entity framework. You add an entity framework to your model of your MVC. Then you could easily do something like this with linq:
using(var context = new OracleEntities())
{
var employees = context.Employees.ToArray();
}
Of course you could keep the EF seperate from model objects you already have. So if you already have an Employee class you could also do something like this instead of the above:
var employees = context.Employees.Select(s => new Employee(s.Employee_Id, s.Employee_Name, etc).ToArray();
Last, I just realized if you already have a DataTable, you can still use linq. Just do dt.AsEnumerable().ToArray(); That would product an array of DataRows, so what you would do to get an array of Employee objects is the same select example above. dt.AsEnumerable.Select(s => new Employee( etc etc)).ToArray().
Hope this helps.
jehrenfeld
0 Points
3 Posts
Re: converting datable to a string[]
Nov 27, 2012 06:41 PM|LINK
Thanks for the reply. I was able to figure out by using dt.AsEnumerable().ToArray(). Thanks for all the other info. I'll look into using the Entity Framework for Oracle. This MVC, RESTful Services, XAML etc is all very new, and I might add, overwhelming for me coming from an ASP.NET programming background and creating websites. Thank goodness for forums like this.
Thanks again.