【发布时间】:2015-06-12 02:30:44
【问题描述】:
我尝试使用Except 方法比较两个列表。但是当我这样做时,我收到一条错误消息:
无法从“Systems.Collections.Generic.List”转换为“System.Linq.IQueryable”
'System.Collections.Generic.List 不包含 'Except' 的定义和最佳扩展方法重载 'System.Linq.Queryable.Except(System.Linq.IQueryable, System.Collections.GEneric.IEnumerable )' 有一些无效的参数
我在尝试Intersect 时也遇到过这种情况。我正在尝试比较已发送列表和结果列表(代码和列表如下所示)并返回没有任何匹配项的项目。所以当我在谷歌上搜索如何做到这一点时,我遇到了 except 方法以及 Intersect。
public class Sent
{
public string Address;
public string Data;
}
public class Result
{
public string AddressOK;
public string DataOK;
}
var sent = new List<Sent>();
sent.Add(new Sent() { Address = linaddr1, Data = lindat1 });
var res = new List<Result>();
res.Add( new Result() { AddressOK = linaddr2, DataOK = lindat2 } );
//linaddr1 and 2, lindat1 and 2 contains the address and data shown in the list below
//taken from another part of the entire program
列表如下所示:
sent res
Address Data Address Data
04004C 55AA55 04004C 55AA55
040004 0720 040004 0720
040037 30
04004A FFFF 04004A FFFF
我只尝试使用此代码:
var diff = sent.Except(res).ToList()
但正如我所提到的,它会导致上述错误。
编辑:我编辑了列表。对此感到抱歉。这只是 res 列表缺少原始列表中的一个或两个或多个项目然后比较两个列表以查看 res 列表中缺少哪个项目的问题。
【问题讨论】:
-
你导入Linq了吗?使用 System.Linq;
-
为什么会有两个完全一样的独立类?只是为了可读性目的?如果
sent和res都是List<Sent>,它是否有效? -
@Loocid - 不,如果两者都是
List<Sent>,它就不起作用。