Return DLinq query result as indexable type

  • Thread starter Thread starter Andrus
  • Start date Start date
A

Andrus

For paged data access I need to convert result of DLinq query to something
(list or array) which can be accessed by integer index.
I use for this metod below.

Is it best solution to use foreach (var e in q) for this?

Andrus.

IList<object> SupplyPageOfData(int lowerPageBoundary, int rowsPerPage)
{
Northwind db = CreateDB();
var oq = from c in db.Customers
select new { name = c.CompanyName, contact =
c.ContactName };
oq = oq.Skip(lowerPageBoundary).Take(rowsPerPage);
var q = oq.ToList();
List<object> list = new List<object>();
foreach (var e in q)
list.Add(e);
return list;
}
 
Andrus said:
For paged data access I need to convert result of DLinq query to something
(list or array) which can be accessed by integer index.
I use for this metod below.

Is it best solution to use foreach (var e in q) for this?

You're converting it to a list twice. Why not just call ToList()?
Admittedly that will return a more strongly typed list - but you could
always call Cast<object>() first to avoid that.

After the call to Skip and Take:

return oq.Cast<object>().ToList();
 
Why not just return IList? (no generic)

Then you can return oq.ToList() - job done.

Marc
 
Back
Top