Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract one property as a List<String> from a ICollection of a Model

I'm trying to select a single property [filename] into a List out of a ICollection where dr405 has many properties.

return GetDR405ById(c, id).dr405files.Select(p => p.FileName).ToList<String>();

     public class dr405files
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int FileId { get; set; }
    public String TangiblePropertyId { get; set; }
    public String FileName { get; set; }
    public DateTime?  UploadDate { get; set; }
    public Byte[] FileData {get;set;}
    public long? FileLength { get; set; }


}

I want the SQL equivalent of SELECT [Column1] FROM [Table1] as opposed to `SELECT * FROM [Table1]

like image 300
Doug Chamberlain Avatar asked Dec 15 '11 15:12

Doug Chamberlain


1 Answers

I think you just want to do

return GetDR405ById(c, id).Select(p => p.FileName).ToList();

unless GetDR405ById really does return an object that has a property called dr405files that is a generic collection of dr405files objects.

EDIT.

Notice i have also removed the generic type param from ToList(). Filename is a string so T will be infered by the compiler.

like image 56
Ben Robinson Avatar answered Sep 18 '22 19:09

Ben Robinson