Processing Search Results
Processing Results for multiple POCO objects
The code fragment below shows how we processed the search results in earlier samples.
foreach (Document doc in res.Documents)
{
CustomerOrders _order = new CustomerOrders()
{
id = doc.Id,
score = doc.Score,
customerName = (string)doc["customerName"],
address = (string)doc["address"],
customerID = (string)doc["customerID"],
productName = (string)doc["productName"],
extendedPrice = Convert.ToDecimal(doc["extendedPrice"]),
//.. etc
};
customerOrdersList.Add(_order);
}
return customerOrdersList;
The disadvantage of this approach is that it is bound to the CustomerOrders class. If you have several classes to which you cast your search results, you'll have to repeat the same kind of code for every one of those classes.
We can improve the code to handle multiple classes by using a generic method to process/cast the results. The trick is let the POCO classes inherit from the same base as shown below for the CustomerOrders class.
Armed with this definition, we can then use the sample code below to call a generic method to cast the search results.
The magic happens in lines #13-14, where we use the generic method GetDocData shown below to cast the search results.
Note: the generic method is constrained to DocCommon.