I am using Linq to SQL.I had dragged and dropped a stored procedure .
In interface i have
IList<SP_Role>GetDetails(string strid)
also i am using Repository to read datas i.e _port is repository.From repositor i call interface.
_port.GetDetails(strid)
I want to convert this to datatable
Datatable dt=null;
dt=(Datatable)_port.GetDetails(strid)
It shows error
Unable to cast object of type 'System.Collections.Generic.List`1[RRPortal.Data_Access_Layer.SP_Validate_ITISAssociatesResult]' to type 'System.Data.DataTable'.
The answer is you can't. It's not reasonable to convert a list of objects to datatable. You can iterate through the list and add rows to the datatable. There might be a different/better way to do this.
To convert list to datatable you have to manually iterate all the elements and needs to add new datarow to the datatable. But it will affect you system performance.
Analyze the need for datatable instead of list of object check for the feasiblity of using list in your need.
If it is necessary to use Datatable then refer the below link,
guhan77
Member
12 Points
225 Posts
Convert to DataTable
Feb 19, 2013 05:55 AM|LINK
Hi,
I am using Linq to SQL.I had dragged and dropped a stored procedure .
In interface i have
IList<SP_Role>GetDetails(string strid)
also i am using Repository to read datas i.e _port is repository.From repositor i call interface.
_port.GetDetails(strid)
I want to convert this to datatable
Datatable dt=null;
dt=(Datatable)_port.GetDetails(strid)
It shows error
Unable to cast object of type 'System.Collections.Generic.List`1[RRPortal.Data_Access_Layer.SP_Validate_ITISAssociatesResult]' to type 'System.Data.DataTable'.
please help
MetalAsp.Net
All-Star
112732 Points
18369 Posts
Moderator
Re: Convert to DataTable
Feb 19, 2013 06:07 AM|LINK
The answer is you can't. It's not reasonable to convert a list of objects to datatable. You can iterate through the list and add rows to the datatable. There might be a different/better way to do this.
Pengzhen Son...
Star
8638 Points
884 Posts
Microsoft
Re: Convert to DataTable
Feb 20, 2013 08:16 AM|LINK
Hi,
As MetalAsp.Net said, you should convert list to datatable or dataset. Here is a sample:
http://www.dreamincode.net/forums/topic/119860-convert-generic-list-to-dataset-w-extension-method-in-c%23/
http://technico.qnownow.com/linq-extension-method-to-convert-list-to-datatable-in-c/
Hope it can help you.
Feedback to us
Develop and promote your apps in Windows Store
matifnadeem
Contributor
4950 Points
1151 Posts
Re: Convert to DataTable
Feb 20, 2013 09:04 AM|LINK
Hi guhan77,
You can iterate through the list and add rows to the datatable. To do so, visit this link to convert LINQ list intro datatable
Hope its help.
Cheers
M Atif Nadeem
Mark as Answer if you got right thing
Read my blog | Follow me on LinkedIn
prabu.raveen...
Contributor
5032 Points
956 Posts
Re: Convert to DataTable
Feb 20, 2013 09:13 AM|LINK
To convert list to datatable you have to manually iterate all the elements and needs to add new datarow to the datatable. But it will affect you system performance.
Analyze the need for datatable instead of list of object check for the feasiblity of using list in your need.
If it is necessary to use Datatable then refer the below link,
http://www.codeproject.com/Articles/83271/Convert-a-Generic-List-to-a-Datatable