比较两种不同类型的列表

本文关键字:列表 同类型 两种 比较 | 更新日期: 2023-09-27 17:49:25

我有2种不同类型的列表,需要比较一下。两个列表都有GUID。

首先我将向您展示错误。

错误10"System.Collections.Generic"。List'不包含'Except'的定义和最佳扩展方法重载'System.Linq. queryable .Except ' TSource>(System.Linq. queryable .Except)。IQueryable TSource>, System.Collections.Generic.IEnumerable TSource>)'有一些无效的参数

我的代码
List<CarViewModel> _GetCarsBeforeMove = icarrepository
    .GetList(x => x.CarId.Equals(interlst.Car2Id) && x.ModelId.Equals(interlst.Model2Id))
    .Select(x => new CarViewModel
    {
        CarId = x.CardId, //This returns this type of Id's list 00000000-0000-0000-0000-000000000001
    }).ToList();

我有另一个列表,这个列表也有Guid的

List<Guid> _GetCarsAfterMove = new List<Guid>();
_GetCarsAfterMove .AddRange(interlst.IntLst); //List has values

现在,我想比较上面两个列表并得到差异

var MovedCars = _GetCarsAfterMove.Except(_GetCarsBeforeMove).ToList(); //Above error shows me here

比较两种不同类型的列表

方法Except期望的是IEnumerable<Guid>,而不是IEnumerable<CarViewModel>

将最后一行改为:

var MovedCars = _GetCarsAfterMove.Except(_GetCarsBeforeMove.Select(c => c.CarId)).ToList();