Wednesday, April 28, 2010

Silverlight Masterclass UK


by DanM
27. April 2010 19:00

The Silverlight Tour comes to the UK – and it’s called the Masterclass!

This 3 day hands-on training with both designer and developer tracks looks awesome and (uniquely) has two expert trainers per course.

Currently scheduled in London, Manchester, and the Midlands for June, all courses also come with the chance to win an xbox 360, and Silverlight Spy licences!

Early bird discount of £100 if you book in May, and if you are a member of #SLUGUK or #nxtgenug there are additional discounts to be had.

Full Details are here: http://silverlightmasterclass.net



Cheers

Friday, April 9, 2010

Huge entities list in batches?

Work with huge list of entities in batches? It has been never easier.

Common programming issue –you have list of 10000 integers or entities (bad programming practice!) in generic list and you need to process them in batches of, let’s say, 250 items per batch.

In the past I solved this problem with following algorithm:

- Sort items per some criteria (in most case sort on identifier)

- Take first chunk of data

- Save last processed id into local variable

- While batch size is more than zero

- Process batch

- Take new chunk of data where identifiers are greater than last processed one

- Set new last processed id

Here is listed code snippet:
//sort list
adIds.Sort((number1, number2) => number1.CompareTo(number2));

int lastProcessedId = 0;

List<> tempList = (adIds.Where(item => item > lastProcessedId)).Take(250).ToList();

while (tempList.Count() > 0)
{
markerAds = AdHandler.GetByIDList(tempList); //items processing (in this case grabbing data from db)

lastProcessedId = tempList[tempList.Count - 1];

tempList = (adIds.Where(item => item < lastProcessedId)).Take(250).ToList();

It looks like few simple lines of code. By power of linq, anonymous types and group by method in c# 3.0 it can be done even simpler and in less lines of code.
var groups = adIds.Select((id, index) => new {GroupID = index/250, ID = id}).GroupBy(x => x.GroupID);

foreach (var group in groups)
{
markerAds = AdHandler.GetByIDList(group.Select(x=>x.ID).ToList());
}

Variable “groups” contains chunks of 250 items per group. Faster, simpler and better.

Cheers