StuartM
StuartM

Reputation: 6823

ASPNET Map a property of an object into new array

I have a service which returns a custom object like this:

public List<Image> Images { get; set; }

The image class is like the following:

public string Url { get; set; }
public string MobilelOverride { get; set; }
public string AltText { get; set; }
public string Attribution { get; set; }
public int? Width { get; set; }
public int? Height { get; set; }

In our view model, I have an array of strings.

public List<String> ProductImageUrls { get; set; }

I am not sure how to convert the List of images into a list of the Image.Url for the ProductImageUrls array.

Upvotes: 1

Views: 167

Answers (1)

Skrface
Skrface

Reputation: 1422

What you want to do is to SELECT the Url property of each Image, and place it into a List of String (because Url is a String).

It is pretty easy using Linq :

ProductImageUrls  = Images.Select(a => a.Url).ToList();

Upvotes: 2

Related Questions