Advertisement
brazentongue

uCommerce categories

Jun 26th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UCommerce.EntitiesV2;
  5.  
  6.  
  7. public class categories
  8. {
  9.     /// <summary>
  10.     /// Gets a list of products that are in all categories specified
  11.     /// </summary>
  12.     /// <param name="categories">a comma-delited string of categories</param>
  13.     public List<Product> GetProductsInCategories(string categories)
  14.     {
  15.         List<Product> productsInCategories = new List<Product>();
  16.         List<List<Product>> allProducts = new List<List<Product>>();
  17.  
  18.         try
  19.         {
  20.             // Break the comma delimited string into an array of filter strings
  21.             string[] filters = categories.Split(',');
  22.  
  23.             foreach (string filter in filters)
  24.             {
  25.                 List<Product> productsInCategory = GetProductsInCategory(filter);
  26.                 allProducts.Add(productsInCategory);
  27.             }
  28.  
  29.             // Create a list of sortable products (required by the FindDuplicates() method below)
  30.             var allSortableProducts = new List<List<SortableProduct>>();
  31.             foreach (List<Product> productList in allProducts)
  32.             {
  33.                 var sortableProducts = new List<SortableProduct>();
  34.                 foreach (Product thisProduct in productList)
  35.                 {
  36.                     var sortableProduct = new SortableProduct {product = thisProduct};
  37.                     sortableProducts.Add(sortableProduct);
  38.                 }
  39.                 allSortableProducts.Add(sortableProducts);
  40.             }
  41.  
  42.             // Filter products that are in all categories
  43.             SortedDictionary<SortableProduct, bool>.KeyCollection common = FindDuplicates(allSortableProducts);
  44.             if (common.Count > 0)
  45.             {
  46.                 // Convert back to a List<Product>
  47.                 List<SortableProduct> sortableProducts = common.ToList();
  48.                 foreach (SortableProduct sortableProduct in sortableProducts)
  49.                 {
  50.                     productsInCategories.Add(sortableProduct.product);
  51.                 }
  52.             }
  53.         }
  54.         catch (Exception ex)
  55.         {
  56.         }
  57.  
  58.         return productsInCategories;
  59.     }
  60.  
  61.     private List<Product> GetProductsInCategory(string category)
  62.     {
  63.         List<Product> productsInCategory = new List<Product>();
  64.  
  65.         try
  66.         {
  67.             Category cat = Category.SingleOrDefault(x => x.Name.ToLower() == category.ToLower());
  68.             if (cat != null)
  69.             {
  70.                 productsInCategory = cat.Products.Where(x => x.DisplayOnSite).ToList();
  71.             }
  72.         }
  73.         catch (Exception ex)
  74.         {
  75.             productsInCategory = null;
  76.         }
  77.         return productsInCategory;
  78.     }
  79.  
  80.     /// <summary>
  81.     /// Gets the items that are common to all lists in the list of items
  82.     /// Taken from http://stackoverflow.com/questions/41159/fastest-way-to-find-common-items-across-multiple-lists-in-c-sharp?rq=1
  83.     /// </summary>
  84.     private SortedDictionary<T, bool>.KeyCollection FindDuplicates<T>(List<List<T>> items)
  85.     {
  86.         SortedDictionary<T, bool> current_common = new SortedDictionary<T, bool>(),
  87.             common = new SortedDictionary<T, bool>();
  88.  
  89.         foreach (List<T> list in items)
  90.         {
  91.             if (current_common.Count == 0)
  92.             {
  93.                 foreach (T item in list)
  94.                 {
  95.                     common[item] = true;
  96.                 }
  97.             }
  98.             else
  99.             {
  100.                 foreach (T item in list)
  101.                 {
  102.                     if (current_common.ContainsKey(item))
  103.                     {
  104.                         common[item] = true;
  105.                     }
  106.                 }
  107.             }
  108.  
  109.             if (common.Count == 0)
  110.             {
  111.                 current_common.Clear();
  112.                 break;
  113.             }
  114.  
  115.             SortedDictionary<T, bool> swap = current_common;
  116.  
  117.             current_common = common;
  118.             common = swap;
  119.             common.Clear();
  120.         }
  121.  
  122.         return current_common.Keys;
  123.     }
  124. }
  125.  
  126. public class SortableProduct : IComparable
  127. {
  128.     public Product product;
  129.  
  130.     public int ProductId
  131.     {
  132.         get { return product.Id; }
  133.     }
  134.  
  135.     public int CompareTo(object obj)
  136.     {
  137.         SortableProduct item = obj as SortableProduct;
  138.         if (item != null)
  139.         {
  140.             SortableProduct other = item;
  141.             return other.ProductId.CompareTo(this.ProductId);
  142.         }
  143.         else
  144.         {
  145.             return 0;
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement