How can i change IEnumerable to datatable
I used this but did not work :
Code:
Dim IENUM As IEnumerable = dat.getDanhSachDongSP
Dim bid as New BindingSource
bid.DataSource = IENUM
Dim dt2 As DataTable
dt2 = bid.DataSource
It show error "cant convert from IEnumberable to datatable"
I see that error but i cant fix it. Help me please !!!!! :bigyello:
Re: How can i change IEnumerable to datatable
You can't. You cannot simply change an object of one type into an object of another type. I don't know what the return type of dat.getDanhSachDongSP is but the DataTable class doesn't implement the IEnumerable interface so what you're trying to do is simply not possible. If you want a DataTable then you need to populate a DataTable. You can't just turn some other object that isn't a DataTable into one.
So, what exactly DOES dat.getDanhSachDongSP return? If it's a DataView then you can get the DataTable it views from its Table property. I'm guessing that that's the case because, in order for that object to be assignable to the DataSource of a BindingSource, it must implement IList rather than just IEnumerable. That really begs the question, why are you assigning the result of dat.getDanhSachDongSP to an IEnumerable variable if it's an IList?
Re: How can i change IEnumerable to datatable
No. Dat.GetdanhsachdongSP is IEnumerable type.
I use LINQ and ADO.NET simultanousely to call storeprocedure.
I use LINQ fatster ADO.NET so i used it.
I want to get result datatable from that IEnumerable type .
This is code :
Code:
public IEnumerable<prGetDanhSachDongSPResult> getDanhSachDongSP()
{
try
{
QLBHDataContext data = new QLBHDataContext(getConnectionString()); ;
return data.prGetDanhSachDongSP();
}
catch (Exception ex)
{
throw new Exception("Thông báo lỗi hệ thống:\n" + ex.Message);
}
}
Re: How can i change IEnumerable to datatable
don't return an IEnumerable type... return a List<T> type instead... then add .ToList onto the end of your LINQ call... since it'sreturned as a list, it will implement the IList interface, which is what is necessary for you to bind it to the grid.
-tg
Re: How can i change IEnumerable to datatable
What's the point of using LINQ to SQL if you want a DataTable? LINQ to SQL is not faster than ADO.NET. If you want a DataTable then just use ADO.NET and you'll get a DataTable.
That said, LINQ to SQL was invented because it offers many advantages over using ADO.NET directly. If you're going to use LINQ to SQL then use it and forget about DataTables.
With that said, you're going to need an IList, not an IEnumerable, in order to use data-binding. You can do as tg says but I'd actually say that it's fine to return an IEnumerable and then convert that to an IList outside the getDanhSachDongSP method rather than inside it. You can do it either way, but consider this: a generic List doesn't provide the best support for data-binding. In order to have full data-binding support you need an IBindingList, which a generic List is not. If you want that full support then you'd need to create an ObservableCollection or maybe a BindingList, which you would almost certainly do outside your method, e.g.
vb.net Code:
Dim dataSource As New ObservableCollection(Of prGetDanhSachDongSPResult)(dat.getDanhSachDongSP())
bid.DataSource = dataSource