Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. public class RecommenderContentItem
  2. {
  3. public Guid Id { get; set; }
  4. public List<Tag> Tags { get; set; } //A tag is an object which only contains an Id and value, not relevant to the question.
  5. public double AverageRating { get; set; }
  6. }
  7.  
  8. public static List<Guid> FilterOnContent<T>(List<T> ratedItems,
  9. List<T> itemsToFilter)
  10. {
  11. var cnv = new RciConverter();
  12.  
  13. var ratedRcis = new List<RecommenderContentItem>();
  14. var toFilterRcis = new List<RecommenderContentItem>();
  15. if (typeof(T) == typeof(Restaurant))
  16. {
  17. var rated = (IEnumerable<Restaurant>)ratedItems;
  18. var toFilter = (IEnumerable<Restaurant>)itemsToFilter;
  19.  
  20. ratedRcis = cnv.ConvertMany(rated).ToList();
  21. toFilterRcis = cnv.ConvertMany(toFilter).ToList();
  22. }
  23. if (typeof(T) == typeof(AlgorithmRestaurant))
  24. {
  25. var rated = (IEnumerable<AlgorithmRestaurant>)ratedItems;
  26. var toFilter = (IEnumerable<AlgorithmRestaurant>)itemsToFilter;
  27.  
  28. ratedRcis = cnv.ConvertMany(rated).ToList();
  29. toFilterRcis = cnv.ConvertMany(toFilter).ToList();
  30. }
  31. if (typeof(T) == typeof(Dish))
  32. {
  33. var rated = (IEnumerable<Dish>)ratedItems;
  34. var toFilter = (IEnumerable<Dish>)itemsToFilter;
  35.  
  36. ratedRcis = cnv.ConvertMany(rated).ToList();
  37. toFilterRcis = cnv.ConvertMany(toFilter).ToList();
  38. }
  39. if (!ratedRcis.Any() || !toFilterRcis.Any())
  40. throw new TypeArgumentException("Invalid type."); //Custom exception written by John Skeet.
  41.  
  42. return ContentBasedFilter.Filter(ratedRcis, toFilterRcis).Select(rci => rci.Id).ToList();
  43. }
  44.  
  45. public class RciConverter : IConverter<Restaurant, RecommenderContentItem>,
  46. IConverter<AlgorithmRestaurant, RecommenderContentItem>,
  47. IConverter<Dish, RecommenderContentItem>
  48. {
  49. //All 6 methods for converting/mapping here...
  50. }
  51.  
  52. public interface IConverter<in TSource, out TDestination>
  53. where TSource : class
  54. where TDestination : class
  55. {
  56. IEnumerable<TDestination> ConvertMany(IEnumerable<TSource> sourceObjects);
  57. TDestination Convert(TSource sourceObject);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement