需求:
/// 对同一个类型的两个序列(或叫集合,数组) users,users1进行比较,
/// users1中的元素如果users中没有,就把这些元素与users的元素组合.
解决方案:
Code
using System;
using System.Collections.Generic;
using System.Linq;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
namespace XMLSpy.Linq
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 对同一个类型的两个序列(或叫集合,数组) users,users1进行比较,
/// users1中的元素如果users中没有,就把这些元素与users的元素组合.
/// Author : xmlspy.spring@gmail.com
/// </summary>
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public class SetCompare
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public SetCompare()
{
List<User> users = new List<User>()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
new User(0),
new User(3),
new User(1),
new User(8)
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
List<User> users1 = new List<User>()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
new User(0),
new User(1),
new User(3),
new User(5)
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("------------方式1---------------");
//方式1:使用扩展方法,必须实现IEqualityComparer<T>接口
//users1中的元素如果users中没有,就把这些元素与users的元素组合.
List<User> users2 = users.Union(users1, new UserEqualityComparer()).ToList();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach (User u in users2)
{
Console.WriteLine(u.Id);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////// 重新初始化
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
users = new List<User>()
{
new User(0),new User(3),
new User(1),new User(8)
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
users1 = new List<User>()
{
new User(0),new User(1),
new User(3),new User(5)
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("------------方式2---------------");
//方式2:使用Linq语句,不用实现IEqualityComparer<T>接口
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//步骤1:查找出users1中有而users中没有的User序列
var users3 = from user1 in users1
let tem = from user in users select user.Id
where tem.Contains(user1.Id) == false
select user1;
//步骤2:添加步骤1的结果到users中
users.AddRange(users3);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach (User u in users)
{
Console.WriteLine(u.Id);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public class UserEqualityComparer : IEqualityComparer<User>
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public bool Equals(User x, User y)
{
return x.Id == y.Id;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int GetHashCode(User obj)
{
return obj.Id;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public class User
{
private int _id;
private string _name;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Constructors#region Constructors
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public User(int id)
{
_id = id;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Properties#region Properties
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Id
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _id; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _id = value; }
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Name
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _name; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _name = value; }
}
#endregion
}
}