CUserInfo cuers2 = new CUserInfo() { UserId = 2, UserName = "dsg", LoginIp = "192.168.1.1" };
CUserInfo cuers3 = new CUserInfo() { UserId = 3, UserName = "wfvg", LoginIp = "192.168.1.1" };
CUserInfo cuers6 = new CUserInfo() { UserId = 1, UserName = "wfvg", LoginIp = "192.168.1.3" };
CUserInfo cuers7 = new CUserInfo() { UserId = 7, UserName = "ware", LoginIp = "192.168.1.3" };
CUserInfo cuers21 = new CUserInfo() { UserId = 2, UserName = "ettfg", LoginIp = "192.168.1.4" };
CUserInfo cuers22 = new CUserInfo() { UserId = 8, UserName = "wfhgugg", LoginIp = "192.168.1.4" };
现在有很多list<CUserInfo>放到dictionary<int,list<CUserInfo>> 中,如何将它们userid相同的list<CUserInfo>进行合并为
UserId = 1, UserName = "wdng", LoginIp = "192.168.1.1"
UserId = 2, UserName = "dsg", LoginIp = "192.168.1.1"
UserId = 3, UserName = "wfvg", LoginIp = "192.168.1.1"
UserId = 7, UserName = "ware", LoginIp = "192.168.1.3"
UserId = 8, UserName = "wfhgugg", LoginIp = "192.168.1.4"
这样的一个集合中,放入一个新的dictionary中
ps:这个list有很多个,不止这三个。
4 个解决方案
#1
首先定义一个比较器(具体规则可根据你的实际情况做修改)
然后调用Linq(.net 3.5及以上版本支持)的Union()方法即可:
public class MyEqualityComparer<T> : IEqualityComparer<T>
{
public bool Equals(T x, T y)
{
CUserInfo a = x as CUserInfo;
CUserInfo b = y as CUserInfo;
if (a.UserId == b.UserId)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(T obj)
{
CUserInfo a = obj as CUserInfo;
return a.UserId.GetHashCode();
}
}
然后调用Linq(.net 3.5及以上版本支持)的Union()方法即可:
List<CUserInfo> sList1 = new List<CUserInfo>
{
new CUserInfo { UserId = 1, UserName = "wdng", LoginIp = "192.168.1.1" },
new CUserInfo { UserId = 2, UserName = "dsg", LoginIp = "192.168.1.1" }
};
List<CUserInfo> sList2 = new List<CUserInfo>
{
new CUserInfo() { UserId = 1, UserName = "asdf", LoginIp = "192.168.1.3" },
new CUserInfo { UserId = 7, UserName = "ware", LoginIp = "192.168.1.3" }
};
List<CUserInfo> sList3 = new List<CUserInfo>
{
new CUserInfo() { UserId = 1, UserName = "asdf", LoginIp = "192.168.1.3" },
new CUserInfo { UserId = 7, UserName = "ware", LoginIp = "192.168.1.3" }
};
List<CUserInfo> uList = new List<CUserInfo>();
MyEqualityComparer<CUserInfo> myec = new MyEqualityComparer<CUserInfo>();
uList = sList1.Union<CUserInfo>(sList2, myec).Union<CUserInfo>(sList3, myec).ToList<CUserInfo>();
foreach (var item in uList)
{
Console.WriteLine(item.UserId + "---" + item.UserName + "---" + item.LoginIp);
}
//输出结果:
//1---wdng---192.168.1.1
//2---dsg---192.168.1.1
//7---ware---192.168.1.3
#2
如果要并集的list中,是这两个对象怎么办?
UserId = 11, UserName = "ettfg", LoginIp = "192.168.1.4"
UserId = 12, UserName = "wfhgugg", LoginIp = "192.168.1.4"
他们与uList没有任何交集,如果没有任何交集的我会重新放到一个新的list中,而不是将它们排除掉~~~
而且要合并的list集合有很多很多,你不会要我一个一个union下去吧?
UserId = 11, UserName = "ettfg", LoginIp = "192.168.1.4"
UserId = 12, UserName = "wfhgugg", LoginIp = "192.168.1.4"
他们与uList没有任何交集,如果没有任何交集的我会重新放到一个新的list中,而不是将它们排除掉~~~
而且要合并的list集合有很多很多,你不会要我一个一个union下去吧?
#3
Union()//并集
Except()//差集
Concat()//连接
或者先用个循环把dictionary<int,list<CUserInfo>>中所有的list中的元素装到一个list中。
然后Distinct()就可以了。
Except()//差集
Concat()//连接
或者先用个循环把dictionary<int,list<CUserInfo>>中所有的list中的元素装到一个list中。
然后Distinct()就可以了。
#4
以前在日企的时候,看过java的他们写了一个方法sortMatch();可惜忘了。
嵌套循环可以吗?
嵌套循环可以吗?
#1
首先定义一个比较器(具体规则可根据你的实际情况做修改)
然后调用Linq(.net 3.5及以上版本支持)的Union()方法即可:
public class MyEqualityComparer<T> : IEqualityComparer<T>
{
public bool Equals(T x, T y)
{
CUserInfo a = x as CUserInfo;
CUserInfo b = y as CUserInfo;
if (a.UserId == b.UserId)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(T obj)
{
CUserInfo a = obj as CUserInfo;
return a.UserId.GetHashCode();
}
}
然后调用Linq(.net 3.5及以上版本支持)的Union()方法即可:
List<CUserInfo> sList1 = new List<CUserInfo>
{
new CUserInfo { UserId = 1, UserName = "wdng", LoginIp = "192.168.1.1" },
new CUserInfo { UserId = 2, UserName = "dsg", LoginIp = "192.168.1.1" }
};
List<CUserInfo> sList2 = new List<CUserInfo>
{
new CUserInfo() { UserId = 1, UserName = "asdf", LoginIp = "192.168.1.3" },
new CUserInfo { UserId = 7, UserName = "ware", LoginIp = "192.168.1.3" }
};
List<CUserInfo> sList3 = new List<CUserInfo>
{
new CUserInfo() { UserId = 1, UserName = "asdf", LoginIp = "192.168.1.3" },
new CUserInfo { UserId = 7, UserName = "ware", LoginIp = "192.168.1.3" }
};
List<CUserInfo> uList = new List<CUserInfo>();
MyEqualityComparer<CUserInfo> myec = new MyEqualityComparer<CUserInfo>();
uList = sList1.Union<CUserInfo>(sList2, myec).Union<CUserInfo>(sList3, myec).ToList<CUserInfo>();
foreach (var item in uList)
{
Console.WriteLine(item.UserId + "---" + item.UserName + "---" + item.LoginIp);
}
//输出结果:
//1---wdng---192.168.1.1
//2---dsg---192.168.1.1
//7---ware---192.168.1.3
#2
如果要并集的list中,是这两个对象怎么办?
UserId = 11, UserName = "ettfg", LoginIp = "192.168.1.4"
UserId = 12, UserName = "wfhgugg", LoginIp = "192.168.1.4"
他们与uList没有任何交集,如果没有任何交集的我会重新放到一个新的list中,而不是将它们排除掉~~~
而且要合并的list集合有很多很多,你不会要我一个一个union下去吧?
UserId = 11, UserName = "ettfg", LoginIp = "192.168.1.4"
UserId = 12, UserName = "wfhgugg", LoginIp = "192.168.1.4"
他们与uList没有任何交集,如果没有任何交集的我会重新放到一个新的list中,而不是将它们排除掉~~~
而且要合并的list集合有很多很多,你不会要我一个一个union下去吧?
#3
Union()//并集
Except()//差集
Concat()//连接
或者先用个循环把dictionary<int,list<CUserInfo>>中所有的list中的元素装到一个list中。
然后Distinct()就可以了。
Except()//差集
Concat()//连接
或者先用个循环把dictionary<int,list<CUserInfo>>中所有的list中的元素装到一个list中。
然后Distinct()就可以了。
#4
以前在日企的时候,看过java的他们写了一个方法sortMatch();可惜忘了。
嵌套循环可以吗?
嵌套循环可以吗?