Showing posts with label lambda. Show all posts
Showing posts with label lambda. Show all posts

Thursday, August 20, 2009

Generic List

So I was asked today how to search a generic list. Apparently the functionality in generics is not as wide spread known as I had previously thought. Being that I do a lot generic list searching for the Amazon and eBay projects I have been working on I thought I would post a couple of simple examples on how to use what I thought was the core and most important functionality available with generics. Since I am using the same data but sending different to multiple locations, I load my relational object data into searchable generic lists.

So in this example we are using the List<>.Find method to retrieve a SINGLE item from the list matching the predicate defined in the expression. This was made much easier with the release of 3.5.


InventoryItemSubmit item = messages.Find(p => p.Sku == _sku);


In this example we are looking for a single InventoryItemSubmit by matching the SKU or unique key with a particular unique key. This method will search the entire list and find the single item, if exists, that has the sku defined second in the expression. You say why not use a lambda or LINQ query? Well in this case I will have over 500K items in the list. I found in testing that the LINQ query will often through an OutOfMemory exception with large quantities of data.

This second example:


List r = results.Rows.FindAll(p => Convert.ToInt32(p["SKU"].Value) == 320934);


uses the FindAll method to retrieve a list of objects that match the criteria. So in this case we are searching a custom result set to find all the rows with a SKU or unique id that match the id 320934.

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);
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.