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);
reader.Close();
reader.Dispose();
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:
Post a Comment