Wednesday, January 07, 2009

Adding a row number using Linq To Objects

This was a nice little trick for working out a row number in a recordset.

Just use a rowNumber and a post increment :)

                List<LinqToSlodgeRoutes.SlodgeRoutes_Activity> list = linqer.Collect(myUserId);

 

                ListBoxItems.DataTextField = "Text";

                ListBoxItems.DataValueField = "Value";

                int rowNumber = 0;

                ListBoxItems.DataSource = (from x in list

                                          select new

                                          {

                                              Value =  rowNumber++,

                                              Text = string.Format("{2:dd MMM yy} - {2:HH:mm} - {0} - {1} - {3:N2} km", x.Type, x.SubType, x.StartTime, (x.DistanceMetres / 1000.0))

                                          }).Reverse();

 


1 comment:

  1. This does not work:

    Expression tree cannot have an assignment operator.

    ReplyDelete