如果这就是你想要的,看看合适不: List<char> SetA = new List<char>() { 'a', 'b', 'c', 'd' }; List<char> SetB = new List<char>() { 'a', 'c', 'e' };var result = SetA.Except(SetA.Intersect(SetB)).Union(SetB.Except(SetA.Intersect(SetB))).ToList();foreach (var item in result) Console.WriteLine(item); 结果: b d e
就把这句sql语句写成Linq,请指教!select * from ListUser1 as a where a.userId not in (select * from ListUser2 as b where a.UserId == b.userId)
不是,我要求的是其中一个的一部分不是两个List 的剩余部分。即:b,d或者e;并不是b,d且e。
那就把这个拆成2个集合 var result = SetA.Except(SetA.Intersect(SetB)).Union(SetB.Except(SetA.Intersect(SetB))).ToList(); b、d var result1 = SetA.Except(SetA.Intersect(SetB)); e var result = SetB.Except(SetA.Intersect(SetB));
ListUser:a,b,c,d
ListAccount:a,c,e
这两个List是有关系的都是泛型,里面只能存放User对象。ListUser和ListAccount中有相同的部分a,c
而我要得到ListUser中的b,d或者ListAccount中的e.
算了,我另举个例子:
ListUser1:a,b,c,d
listUser2:a,c,e
我想得到listUser2中的e。
且Except()这个方法我试过,也不行。就是不知道哪里写错了。请简单代码明示,谢谢!
List<char> SetA = new List<char>() { 'a', 'b', 'c', 'd' };
List<char> SetB = new List<char>() { 'a', 'c', 'e' };var result = SetA.Except(SetA.Intersect(SetB)).Union(SetB.Except(SetA.Intersect(SetB))).ToList();foreach (var item in result)
Console.WriteLine(item);
结果:
b
d
e
var result = SetA.Except(SetA.Intersect(SetB)).Union(SetB.Except(SetA.Intersect(SetB))).ToList();
b、d
var result1 = SetA.Except(SetA.Intersect(SetB));
e
var result = SetB.Except(SetA.Intersect(SetB));