Last post Jun 24, 2011 11:37 AM by harrythedeveloper
Jun 24, 2011 10:23 AM|harrythedeveloper|LINK
I am working on refining the search result based on an existing search result. When the user search, he got some results, then he want to refine the result by see brands or color. My question is how to hold the initial result and do the refining? How do
people usually do this?
An example is this:
On the left side, there is count besides each filter, and when you click the filter, it shows you just those result. Can someone tell me how this is usually being done?
Thanks a lot.
Jun 24, 2011 10:49 AM|Huske|LINK
It depends on the type that holds your result and how your return that result. For example, if your are returning the result set as IQueryable you can use something like this:
IQueryable result = LoadDataFromRepository();
result = result.Where(m => m.FirstName == firstName);
result = result.Where(m => m.LastName == lastName);
Returning a value as IQueryable allows LINQ to build a query in the background and when you execute foreach or call Count() method, the query will execute and return the result.
Jun 24, 2011 11:19 AM|harrythedeveloper|LINK
Thanks for the response! The existing code didn't use Linq. It used stored procs and DataSets. Currently it has 2 filters built in and it actually return the results datatable for those two filters.
But what I need to do now is add more filters on the left, price range, color, size, ..., and for price it might have (0-10, 11-20, ...), each need to have a count and a subsets. If I return a datatable for each of the filter,criteria, then it would be a
lot of work and slow down the query quite a lot.
Jun 24, 2011 11:31 AM|bkotvis|LINK
I would get the initial result to feed the listview or whatever you are using and then group by the category on the same collection to get the count to provide the source for the category counts.
IList result = LoadDataFromRepository();
//bind your listview or whatever with the result.
listview.DataSource = result;
//then group the result to get the categories
var groupedResult = result.GroupBy(i => i.CategoryName);
//you can use the grouped result as a datasource or iterate like this
foreach(var g in groupedResult)
string categoryName = g.Key.CategoryName;
int categoryCount = g.Count();
Jun 24, 2011 11:37 AM|harrythedeveloper|LINK
Thanks! I will try that.