将列表转换为字符串列表

时间:2012-11-09 19:31:56

标签: c# linq entity-framework c#-4.0

请查看代码:

// get column list from entity framework
var listID = from Ines in ineContext.IneDetailRecords
             select Ines.InePIN.ToString().ToList();
string PIN = something;
if(!listID.Contains(PIN))
    // save it to DB

InPIN是SQL Server DB中的一列,类型为varchar(20)。

例外是:

Instance argument: cannot convert from 'System.Linq.IQueryable<System.Collections.Generic.List<char>>' to 'System.Linq.ParallelQuery<string>'

2 个答案:

答案 0 :(得分:6)

您需要对查询结果应用ToList方法,但不要对所选列应用ToList方法,即Ines.InePIN.ToString().ToList()

更改

var listID = from Ines in ineContext.IneDetailRecords
                                       select Ines.InePIN.ToString().ToList();

var listID = (from Ines in ineContext.IneDetailRecords
                                       select Ines.InePIN.ToString()).ToList();

答案 1 :(得分:1)

您必须转换整个列表ToString()

var listID = (from Ines in ineContext.IneDetailRecords
             select Ines.InePIN.ToString()).ToList();

并查看此链接以更好地理解类型协方差http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx