Using LINQ how do I create a List of one particular field of an entity from a collection entities

c#, lambda, linq, list

Solution

Use `Enumerable.Select` and `Enumerable.ToList`.

List<String> listOfCodes = listOfProviders
                     .Select(p => p.Code)
                     .ToList();

Problem

If I have the following collection ``` IEnumerable<ProviderOrganisation> listOfProviders public class ProviderOrganisation { public virtual string Code { get; set; } public virtual string Description { get; set; } public virtual int SortOrder { get; set; } public virtual bool IsDefault { get; set; } public virtual DateTime EffectiveStartDate { get; set; } public virtual DateTime? EffectiveEndDate { get; set; } } ``` how do I write the LINQ to produce a collection of just Codes please? So, just a List of the Codes: ``` List<string> listOfCodes ``` Thanks

Original source