Syntax for linq query to List<string>

主宰稳场 提交于 2019-12-10 01:20:59

问题


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


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



回答2:


try this,

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

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



回答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!!



来源:https://stackoverflow.com/questions/7488980/syntax-for-linq-query-to-liststring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!