Syntax for linq query to List<string>

24,574

Solution 1

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;
}

Solution 2

try this,

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

    return dc.Attachments.Select(a=>a.Att_Key).ToList();
}

Solution 3

I guess it would something like below.

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

Hope this helps!!

Share:
24,574
MalamuteMan
Author by

MalamuteMan

Updated on August 08, 2022

Comments

  • MalamuteMan
    MalamuteMan almost 2 years

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