Remembering that LINQ to SharePoint often requires Two-Stage Queries, I decided to employ a similar approach here and pull back the data I could then perform more culling server side.
DataTable searchResults = PerformSearch(query, queryRowLimit); if (searchResults != null && searchResults.Rows.Count > 0) { int limit = 10; //todo: this should be a webpart property var results = (from row in searchResults.AsEnumerable() orderby Guid.NewGuid() select row).Take(limit); }
The PerformSearch method is just a standard setup for using FullTextSqlQuery to return a DataTable. Note that I limit my resultset as much as I can with the FullTextSqlQuery.QueryText property to try to avoid hitting a throttling exception.
With the limited results returned, I then want to randomize the order of the records. LINQ allows us to do this the same way we would in SQL, and I just order by a random Guid. Once the results are randomized, we just grab the number of records we need with the Take() extension method.
No comments:
Post a Comment