我有一个列表
List<PossibleSolutionCapacitors> PossibleSolution = new List<PossibleSolutionCapacitors>();
这是它的类
class PossibleSolutionCapacitors
{
public int CapacitorALocation { get; set; }
public int CapacitorBLocation { get; set; }
public int CapacitorCLocation { get; set; }
}
我有3个整数
int A;
int B;
int C;
我需要检查列表中是否包含A,B,C的任何组合
即如果列表中有以下内容(布尔说真/假就足够了)
这可能吗?
感谢 达莫
答案 0 :(得分:3)
Save的解决方案的变体:
var fixedSet = new HashSet<int>(){A,B,C};
bool result = PossibleSolutions.Any(x => fixedSet.SetEquals(
new[] { x.CapacitorALocation,x.CapacitorBLocation,x.CapacitorCLocation }));
答案 1 :(得分:2)
var query = PossibleSolution.Any(x=>HashSet<int>.CreateSetComparer()
.Equals(new HashSet<int>(){A,B,C}
,new HashSet<int>(){x.CapacitorALocation,x.CapacitorBLocation,x.CapacitorCLocation}));
为了节省时间,您可以预先创建HashSet<int>(){A,B,C}
和比较器,并在代码中调用它,例如:
var fixedSet = new HashSet<int>(){A,B,C};
IEqualityComparer<HashSet<int>> comparer = HashSet<int>.CreateSetComparer();
var query = PossibleSolution.Any(
x=>comparer.Equals(fixedSet,new HashSet<int>(){x.CapacitorALocation,x.CapacitorBLocation,x.CapacitorCLocation}));
最后,对于使用SetEquals
而不是比较器的版本,请查看Thomas Levesque解决方案。