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
Use Enumerable.Select and Enumerable.ToList.
List<String> listOfCodes = listOfProviders
                     .Select(p => p.Code)
                     .ToList();
You can call the Select() method to pull out only the property you need. Then call ToList() to cast it.
listOfProviders.Select(p => p.Code).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With