简体   繁体   中英

best way to convert WCF datatable to generic/LINQ object for silverlight use

I have one WCF which i have used in my project. Now i want to use it on my other silverlight project also.

The WCF is set to return the datatable as per the query given by the parameter. Its not returning single type of datatable so i cannot map it to object of one type only, the returned datatable's columns are defined by the query which is pass as the parameter.

Now i want to use the same WCF in my silverlight code, i know that i cannot use the datatable into silverlight project. As i have to convert this datatable into generic list/LINQ object of ienuberable/List.

Right now i am using the below code to do the transformation from datatable to IEnumerable>

var columns = table.Columns.Cast<DataColumn>();
return table.AsEnumerable().Select(r => columns.Select(c =>
                             new { Column = c.ColumnName, Value = r[c] })
                         .ToDictionary(i => i.Column, i => i.Value != DBNull.Value ? i.Value : null));

I just want your guys opinion here. Is this the best methods to do the conversation? Please also include the point of memory efficiency as my datatable has more then 200k rows.

Thank you for your time to look into this.

A dictionary is very inefficient in this case because for every row you have to save the Name=Value pairs again.

A simple optimization would be to store an array of values for each columm.

Something like this:

from c as DataColumn in table.Columns
select new { Column=c.ColumnName, Values = table.Select(r => r[c]).ToArray() }

I would also recommend implement paging if you have a large table. So have your WCF service methods have a PageSize and PageIndex parameter for example. Like this you can return the first eg 50 rows, then the next, etc.

In any case you should think if returning a DataTable from the service (transformed or not) is really a good idea. Accepting a query from a method and then returning arbitrary results sounds dangerous. Why not have domain specific methods with the possibility to specify filters. For example GetCustomers(filter, pageIndex, pageSize) . In the filter object you can allow filtering by name, id or whatever you need. Then you can return a really small object.

See also the following post on the topic:

http://www.hanselman.com/blog/PermaLink,guid,d88f7539-10d8-4697-8c6e-1badb08bb3f5.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM