Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Query Except not working, List<long?> vs. List<long>

What I'm trying to do is narrow the results of a query that I will use to databind later on. I want to get all of the ProgramIds that are used in my gridview, and remove them from the datasource of my dropdownlist (ie, so that a user cannot choose to create and insert an object into the gridview of the same ProgramId as one that already exists)

Here's some code:

var query = from goals in DataContext.Goals
            select goals;
var query2 = (from goals in query.AsEnumerable()
              select goals.ProgramId).ToList(); //List<long?>
var query3 = (from progs in DataContext.Programs
              select progs.ProgramId).ToList(); //List<long>
var cgps = query3.Except(query2);

And I'm getting these errors on var cgps = query3.Except(query2); :

Error 29 'System.Collections.Generic.List' does not contain a definition for 'Except' and the best extension method overload 'System.Linq.ParallelEnumerable.Except(System.Linq.ParallelQuery, System.Collections.Generic.IEnumerable)' has some invalid arguments C:...\Shmeh\Shmeh\Shmeh\this.aspx.cs 24 226 Project

Error 30 Instance argument: cannot convert from 'System.Collections.Generic.List' to 'System.Linq.ParallelQuery' C:...\Shmeh\Shmeh\Shmeh\this.aspx.cs 24 226 Project

If you have any idea how to validly do what I'm trying to do, any help would be greatly appreciated! Thanks!

like image 299
Jordan Foreman Avatar asked Dec 03 '22 02:12

Jordan Foreman


2 Answers

Except requires that the sequences are both of the same type. Try casting the long list to long?:

var query3 = (from progs in DataContext.Programs
              select (long?)progs.ProgramId).ToList(); //List<long?>
like image 78
bdukes Avatar answered Dec 24 '22 00:12

bdukes


You're getting this error because long? is not the same type as long. Except requires both enumerable objects to be of the same type.

What you could do is remove the null values from query2 and convert ProgramId to long

var query2 = (from goals in query.AsEnumerable()
              where goals.ProgramId.HasValue
              select goals.ProgramId.Value).ToList()
like image 36
bdowden Avatar answered Dec 24 '22 01:12

bdowden