gpt4 book ai didi

c# - LINQ 查询除了不起作用,List 与 List

转载 作者:太空狗 更新时间:2023-10-30 00:00:24 24 4
gpt4 key购买 nike

我正在尝试做的是缩小稍后将用于数据绑定(bind)的查询的结果。我想获取我的 gridview 中使用的所有 ProgramId,并将它们从我的下拉列表的数据源中删除(即,这样用户就不能选择创建一个对象并将其插入到与已经存在的相同 ProgramId 的 gridview 中存在)

这是一些代码:

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

我在 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

如果您知道如何有效地做我正在尝试做的事情,我们将不胜感激!谢谢!

最佳答案

Except要求序列都是同一类型。尝试将 long 列表转换为 long?:

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

关于c# - LINQ 查询除了不起作用,List<long?> 与 List<long>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6598761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com