比较两种不同类型的列表

时间:2015-05-17 17:06:43

标签: c# asp.net linq

我有2种不同类型的列表,需要对它进行比较。这两个列表都有GUID'

首先我会告诉你错误。

  

错误10' System.Collections.Generic.List'不包含'除外'的定义和最好的扩展方法重载' System.Linq.Queryable.Except TSource>(System.Linq.IQueryable TSource>,System.Collections.Generic.IEnumerable TSource>)'有一些无效的论点

我的代码

#include <stdio.h>

int arr[100]={0,1,2,3,4,5,6,7,8,9}, start=0, end=9, i=0;

int BinaryRecursivSearch(int element, int start, int end)
{
    int mid = (start + end)/2;

    i++;

    printf("\n%d\nmid = %d\nstart = %d\nend = %d\n\n", i, mid,start,end);

    if (mid == start)
    {
        if(element!=end)
        {
            printf("\nElement not found");
            return -1;
        }
    }

    else if (element < arr[mid])
        BinaryRecursivSearch(element, start, mid);

    else if (element > arr[mid])
        BinaryRecursivSearch(element, mid, end);

    else if (element == arr[mid])
    {

//For the last iteration (or recursion) mid has the correct value in printf

printf("\n%d\nmid = %d\nstart = %d\nend = %d\n\n", i, mid,start,end);
        return mid;  //It skips this
    }

    return 0;  //It returns this value
}

int main()
{
    int element, index;

    printf("Enter the element to be searched for: ");
    scanf("%d", &element);

    index = BinaryRecursivSearch(element, start, end);

    printf("\n Element found at %d position", index + 1 );

    return 0;

}

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

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

现在,我想比较以上2个列表并获得差异

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

1 个答案:

答案 0 :(得分:1)

方法Except需要IEnumerable<Guid>,而不是IEnumerable<CarViewModel>

将最后一行更改为:

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