比较由实体框架查询结果在C#中得出的等式2列表

涂鸦假名

我的密码

    query1 = select value1, value2 from dbo.animal where animalname="tiger";
    query2 = select value1, value2 from dbo.animal where animalname="lion";
    List<Animal> list1 = db.ExecuteStoreQuery<Animal>(query1).ToList();
    List<Animal> list2 = db.ExecuteStoreQuery<Animal>(query2).ToList();
    // list1.Count = 1 list1[0].value1 = "a" list1[0].value2 = "b"
    // list2.Count = 1 list2[0].value1 = "a" list2[0].value2 = "b"
    // Now I would like to compare list1 and list2 to see if they are equal
    var list3 = list1.Except(list2);
   //At this point list3 is containing both list1 and list2 and using var made it
 //GenericList not the same type of list as List1 and List2

我尝试了List3 = list1.Except(list2),但出现编译错误。

所以问题是如何找出list1是否等于list2?我希望list3是list1和list2之间的差异,因此,如果列表相等,则list3.count()应该为0。

关于我的数据的好处是,我相信来自query1和query的数据都应有序,并且每个记录仅包含1条记录。

谢尔盖·卡里尼琴科(Sergey Kalinichenko)

首先,检查的结果是否Except为空不能回答“这两个列表是否相等?” 问题。它回答“是否list2包含list1?的所有元素”,这是不一样的,因为list2 可能包含其他元素。

关于我的数据的好处是,我相信来自query1和query的数据都应有序,并且每个记录仅包含1条记录。

您可以使用来比较两个相同顺序排列的列表是否相等SequenceEqual,如下所示:

bool areIdentical = list1.SequenceEqual(list2);

如果顺序不同,您还可以通过以下命令强制将其相同OrderBy

bool areIdentical = list1
    .OrderBy(animal=>animal.Id)
    .SequenceEqual(list2.OrderBy(animal=>animal.Id));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章