Return DLinq query result as indexable type

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;
}
 
J

Jon Skeet [C# MVP]

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();
 
M

Marc Gravell

Why not just return IList? (no generic)

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

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top