linq 查询到 List<string> 的语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7488980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:13:16  来源:igfitidea点击:

Syntax for linq query to List<string>

stringlinqcastingnvarchar

提问by MalamuteMan

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

Visual Studio 说...

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

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

What is the proper syntax???

什么是正确的语法???

回答by Brian Dishaw

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

回答by Pedro Maia Costa

try this,

尝试这个,

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

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

回答by Praveen

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

希望这可以帮助!!