user1108948
user1108948

Reputation:

Convert a list to a string list

Please look at the code:

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

InePIN is a cloumn in SQL Server DB, the type is varchar(20).

The exception is:

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

Upvotes: 1

Views: 1040

Answers (2)

Adil
Adil

Reputation: 148110

You need to apply the ToList method on result of query but not on the selected column i.e Ines.InePIN.ToString().ToList()

Change

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

To

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

Upvotes: 6

Mihai
Mihai

Reputation: 2760

You must convert the whole list ToString()

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

And have a look at this link for a better understanding of type covariance http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx

Upvotes: 1

Related Questions