Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for linq query to List<string>

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???

like image 732
MalamuteMan Avatar asked Sep 20 '11 17:09

MalamuteMan


2 Answers

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;
}
like image 96
Brian Dishaw Avatar answered Oct 15 '22 06:10

Brian Dishaw


try this,

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

    return dc.Attachments.Select(a=>a.Att_Key).ToList();
}
like image 32
Pedro Costa Avatar answered Oct 15 '22 06:10

Pedro Costa