简体   繁体   中英

Can I use LINQ to skip a collection and just return 100 records?

I have the following that returns a collection from Azure table storage where Skip is not implemented. The number of rows returned is approximately 500.

ICollection<City> a = cityService.Get("0001I");

What I would like to do is to be able to depending on an argument have just the following ranges returned:

records 1-100           passing in 0   as an argument to a LINQ expression
records 101-200         passing in 100 as an argument to a LINQ expression
records 201-300         passing in 200 as an argument to a LINQ expression
records 301-400         passing in 300 as an argument to a LINQ expression
etc

Is there some way I can add to the above and use link to get these ranges of records returned:

As you already stated in your question, the Skip method is not implemented in Windows Azure Table storage. This means you have 2 options left:

Option 1

Download all data from table storage (by using ToList, see abatishchev's answer) and execute the Skip and Take methods on this complete list. In your question you're talking about 500 records. If the number of records doesn't grow too much this solution should be OK for you, just make sure that all records have the same partition key.

If the data grows you can still use this approach, but I suggest you evaluate a caching solution to store all the records instead of loading them from table storage over and over again (this will improve the performance, but don't expect this to work with very large amounts of data). Caching is possible in Windows Azure using:

Option 2

The CloudTableQuery class allows you to query for data, but more important to receive a continuation token to build a paging implementation. This allows you to detect if you can query for more data, the pagination example on Scott's blogpost (see nemensv's comment) uses this.

For more information on continuation tokens I suggest you take a look at Jim's blogpost: Azure@home Part 7: Asynchronous Table Storage Pagination . By using continuation tokens you only download the data for the current page meaning it will also work correctly even if you have millions of records. But you have to know the downside of using continuation tokens:

  • This won't work with the Skip method out of the box, so it might not be a solution for you.
  • No page 'numbers', because you only know if there's more data (not how much)
  • No way to count all records

If paging is not supported by the underlying engine, the only way to implement it is to load all the data into memory and then perform paging:

var list = cityService.Get("0001I").ToList(); // meterialize
var result = list.Skip(x).Take(y);

Try something like this:

cityService.Get("0001I").ToList().Skip(n).Take(100);

This should return records 201-300:

cityService.Get("0001I").ToList().Skip(200).Take(100);
a.AsEnumerable().Skip(m).Take(n)

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