Tuesday, August 18, 2009

eBay Woes

One of the requirements for our eBay implementation was to provide the ability for our marketing department to send and particular set of products up to Channel Advisor. Of course these being non-technical staff, they needed a easy to use and familiar mechanism to create a list of products by their product id, or our internal unique identifier. Excel was the first that came to mind of course and when that recommendation was excepted it reminded me of a class I taught once about loading arrays from Excel. Of course now we have generics so it was an even easier implementation.

Below is the simple code to load a list of product ids from excel into a generic and then use a lambda (LINQ) to get a distinct list of products. The distinct is added because the list is usually copied and pasted. We have what we call skus which are basically the children of products. So if you have a large, medium and small of a shirt, the shirt would be the product id and the skus would be the individual sizes.

StreamReader reader = File.OpenText(@"C:\location of csv.csv");
string[] content = reader.ReadToEnd().Split(new string[1] { Environment.NewLine }, 100000, StringSplitOptions.RemoveEmptyEntries);
List<Int32> ids = new List<int>();

// ... some special code to handle our product ids

IEnumerable<Row> r = (from p in results.Rows
where ids.Contains(Convert.ToInt32(p["SKU"].Value))
select p).Distinct();

Notice we use IEnumberable as the return set with a Row object. Row is a custom serializeable object used internally for faster communication via our internal SOA architecture and IEnumberable is a faster iteration by just a fraction than iterating over the collection of rows via a foreach loop.

The only slow part of this is the Distinct. This is of course an in memory query which can be intensive and large data sets. It is so far working for about 500K products.

No comments: