Syntax for linq query to List<string>

MalamuteMan picture MalamuteMan · Sep 20, 2011 · Viewed 22.9k times · Source

I am trying to do something like this...

public static List<string> GetAttachmentKeyList()
{
    DataClassesDataContext dc = new DataClassesDataContext();

    List<string> list = from a in dc.Attachments
        select a.Att_Key.ToString().ToList();

    return list;
}

Visual Studio is saying...

Cannot implicitly convert type 'System.Linq.IQueryable>' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)

What is the proper syntax???

Answer

Brian Dishaw picture Brian Dishaw · Sep 20, 2011

Give this a try

public static List<string> GetAttachmentKeyList()
{
    DataClassesDataContext dc = new DataClassesDataContext();

    List<string> list = ( from a in dc.Attachments
                          select a.Att_Key.ToString() ).ToList();

    return list;
}