Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UCommerce.EntitiesV2;
- public class categories
- {
- /// <summary>
- /// Gets a list of products that are in all categories specified
- /// </summary>
- /// <param name="categories">a comma-delited string of categories</param>
- public List<Product> GetProductsInCategories(string categories)
- {
- List<Product> productsInCategories = new List<Product>();
- List<List<Product>> allProducts = new List<List<Product>>();
- try
- {
- // Break the comma delimited string into an array of filter strings
- string[] filters = categories.Split(',');
- foreach (string filter in filters)
- {
- List<Product> productsInCategory = GetProductsInCategory(filter);
- allProducts.Add(productsInCategory);
- }
- // Create a list of sortable products (required by the FindDuplicates() method below)
- var allSortableProducts = new List<List<SortableProduct>>();
- foreach (List<Product> productList in allProducts)
- {
- var sortableProducts = new List<SortableProduct>();
- foreach (Product thisProduct in productList)
- {
- var sortableProduct = new SortableProduct {product = thisProduct};
- sortableProducts.Add(sortableProduct);
- }
- allSortableProducts.Add(sortableProducts);
- }
- // Filter products that are in all categories
- SortedDictionary<SortableProduct, bool>.KeyCollection common = FindDuplicates(allSortableProducts);
- if (common.Count > 0)
- {
- // Convert back to a List<Product>
- List<SortableProduct> sortableProducts = common.ToList();
- foreach (SortableProduct sortableProduct in sortableProducts)
- {
- productsInCategories.Add(sortableProduct.product);
- }
- }
- }
- catch (Exception ex)
- {
- }
- return productsInCategories;
- }
- private List<Product> GetProductsInCategory(string category)
- {
- List<Product> productsInCategory = new List<Product>();
- try
- {
- Category cat = Category.SingleOrDefault(x => x.Name.ToLower() == category.ToLower());
- if (cat != null)
- {
- productsInCategory = cat.Products.Where(x => x.DisplayOnSite).ToList();
- }
- }
- catch (Exception ex)
- {
- productsInCategory = null;
- }
- return productsInCategory;
- }
- /// <summary>
- /// Gets the items that are common to all lists in the list of items
- /// Taken from http://stackoverflow.com/questions/41159/fastest-way-to-find-common-items-across-multiple-lists-in-c-sharp?rq=1
- /// </summary>
- private SortedDictionary<T, bool>.KeyCollection FindDuplicates<T>(List<List<T>> items)
- {
- SortedDictionary<T, bool> current_common = new SortedDictionary<T, bool>(),
- common = new SortedDictionary<T, bool>();
- foreach (List<T> list in items)
- {
- if (current_common.Count == 0)
- {
- foreach (T item in list)
- {
- common[item] = true;
- }
- }
- else
- {
- foreach (T item in list)
- {
- if (current_common.ContainsKey(item))
- {
- common[item] = true;
- }
- }
- }
- if (common.Count == 0)
- {
- current_common.Clear();
- break;
- }
- SortedDictionary<T, bool> swap = current_common;
- current_common = common;
- common = swap;
- common.Clear();
- }
- return current_common.Keys;
- }
- }
- public class SortableProduct : IComparable
- {
- public Product product;
- public int ProductId
- {
- get { return product.Id; }
- }
- public int CompareTo(object obj)
- {
- SortableProduct item = obj as SortableProduct;
- if (item != null)
- {
- SortableProduct other = item;
- return other.ProductId.CompareTo(this.ProductId);
- }
- else
- {
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement