开发者

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开发者_如何学C\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!


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


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()


try

var cgps = query3.Cast<long?>().Except(query2);


var cgps = DataContext.Programs.Select(p => p.ProgramId)
    .Except(DataContext.Goals.Where(g => g.ProgramId.HasValue).Select(g => g.ProgramId.Value));


I don't know if this will work for you.

var query2 = (from goals in query.AsEnumerable()
              select goals.ProgramId).ToList() as List<long?>;

var query3 = (from progs in DataContext.Programs
              select (long?)progs.ProgramId).ToList() as List<long?>;


I don't know why he/she deleted their answer, but I'm using this:

dropdownlist.DataSource = (
            from progs in DataContext.Programs
            where !(from goals in query.AsEnumerable()
                    select goals.ProgramId)
                   .Contains(progs.ProgramId)
            select progs.Name).ToList();
dropdownlist.DataBind();

Because it doesn't require me to use multiple query variables, although the answer I accepted worked as well with what I had.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜