gpt4 book ai didi

c# - 如何在 C# 中比较两个 list 并只保留没有重复的项目?
转载 作者:太空狗 更新时间:2023-10-29 22:22:15 24 4
gpt4 key购买 nike

这里有两个列表:

var list1 = new List<UserGroupMap> 
{
new UserGroupMap { UserId = "1", GroupId = "1", FormGroupFlag = "1", GroupDescription = "desc1", GroupName = "g1"},
new UserGroupMap { UserId = "1", GroupId = "2", FormGroupFlag = "1", GroupDescription = "desc1", GroupName = "g1"},
new UserGroupMap { UserId = "1", GroupId = "3", FormGroupFlag = "1", GroupDescription = "desc1", GroupName = "g1"},
new UserGroupMap { UserId = "2", GroupId = "3", FormGroupFlag = "1", GroupDescription = "desc1", GroupName = "g1"}
};

var list2 = new List<UserGroupMap>
{
new UserGroupMap { UserId = "1", GroupId = "1", FormGroupFlag = "1", GroupDescription = "desc1", GroupName = "g1"},
new UserGroupMap { UserId = "1", GroupId = "2", FormGroupFlag = "1", GroupDescription = "desc1", GroupName = "g1"},
new UserGroupMap { UserId = "1", GroupId = "3", FormGroupFlag = "1", GroupDescription = "desc1", GroupName = "g1"},
new UserGroupMap { UserId = "2", GroupId = "3", FormGroupFlag = "1", GroupDescription = "desc1", GroupName = "g1"},
new UserGroupMap { UserId = "4", GroupId = "3", FormGroupFlag = "1", GroupDescription = "desc1", GroupName = "g1"},
new UserGroupMap { UserId = "3", GroupId = "3", FormGroupFlag = "1", GroupDescription = "desc1", GroupName = "g1"},
};

现在我想要的是获得一个没有重复的列表,基本上比较 list1 和 list2 只返回重复的项目。

根据示例,它应该返回的是列表 2 中的最后两项,因为它们不在列表 1 中。

最佳答案

试试这个

list2.Except(list1).Concat(list1.Except(list2));

关于c# - 如何在 C# 中比较两个 list<object> 并只保留没有重复的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30297972/

24 4 0