14

我正在尝试做这样的事情......

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 说...

无法将类型“System.Linq.IQueryable>”隐式转换为“System.Collections.Generic.List”。存在显式转换(您是否缺少演员表?)

什么是正确的语法???

4

3 回答 3

22

试试这个

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;
}
于 2011-09-20T17:12:25.710 回答
12

尝试这个,

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

    return dc.Attachments.Select(a=>a.Att_Key).ToList();
}
于 2011-09-20T17:12:37.817 回答
2

我想它会像下面这样。

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

希望这可以帮助!!

于 2011-09-20T17:12:54.463 回答