2

Was'nt sure of the title, but I have this task which I am having trouble with.

I have a List downloaded from a WCF service.

ie:

 List<Stops_edited_small> StopsList = new List<Stops_edited_small>(e.Result);

The list has several items

two being:

LatitudeField
LongitudeField

Basically, what I need to do is make a Geocoordinate value for each record in the list by doing this:

GeoCoordinate(Convert.ToDouble(LatitudeField), Convert.ToDouble(LongitudeField));

Then add the Geocoordinate value in each record in a new List which I can then use.

Make sense? I am not sure on how I would go about this. Would i need some kind of foreach loop to do the Geocoordinate conversion on my original list someone? Would i have to convert it into a class first to do this?

Thanks, any help or thinking welcome.

(EDIT: just to add this is on windows phone, so .net 4 is available)

Dan Sewell
  • 1,278
  • 4
  • 18
  • 45

2 Answers2

7

You can use the ConvertAll method that goes back to .NET 2.0 before Linq has been introduced.

Here is an example:

Cast List<int> to List<string> in .NET 2.0

And MSDN Article:

http://msdn.microsoft.com/en-us/library/73fe8cwf.aspx

Community
  • 1
  • 1
Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
5

If you are using .NET 3.5 or later you can use LINQ2Objects

var coordinateList = StopsList.Select(stop =>
    new GeoCoordinate(Convert.ToDouble(stop.LatitudeField),
                      Convert.ToDouble(stop.LongitudeField))).ToList();

If you are using an older .NET version you need to use an explicit loop.

var coordinateList = new List<GeoCoordinate>();
foreach(var stop in StopsList)
{
    coordinateList.Add(
        new GeoCoordinate(Convert.ToDouble(stop.LatitudeField),
                          Convert.ToDouble(stop.LongitudeField)));
}

Edit

If you want to combine both the new GeoCoordinate with the stops you have a couple of options.

Either create a list with an anonymous type

var combinedList = StopsList.Select(stop =>
    new {
        s = stop,
        coord = new GeoCoordinate(Convert.ToDouble(stop.LatitudeField),
                      Convert.ToDouble(stop.LongitudeField)),
    }).ToList();

You can also do the same with a class you create, replace new { with new YourClass {.

You can also use the Zip method using both above lists

var combinedList = StopsList.Zip(coordinateList, Tuple.Create).ToList();

This gives you a List<Tuple<Stop, GeoCoordinate>>.

Note that most of the time you can skip .ToList(), you don't get a List<T>, but an IEnumerable<T>. That will work as good as a List in most cases but your program don't have to copy everything to lists all the time. Usually a little bit more efficient and you don't need to type .ToList() all the time.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • Thanks, this is very useful. I think I am halfway there, as I probably need to create a new list and combine the values from the old list to the values from this new list. so basically my old list also has this geocoordinate values in it. Thanks. – Dan Sewell Oct 02 '11 at 13:57
  • Thanks, I used the first suggestion with the new YourClass bit :-) works a treat! – Dan Sewell Oct 02 '11 at 16:41