我有一个列表
List<FirstIterationCapacitors> FirstIteration = new List<FirstIterationCapacitors>(); // A set of Solution capacitors object
这是课堂
class FirstIterationCapacitors
{
public int Iteration { get; set; }
public int CapacitorALocation { get; set; }
public int CapacitorBLocation { get; set; }
public int CapacitorCLocation { get; set; }
}
现在我有第二个清单
List<PossibleSolutionCapacitors> PossibleSolution = new List<PossibleSolutionCapacitors>(); // Possible Solution capacitors object
这是它的类
class PossibleSolutionCapacitors
{
public int CapacitorALocation { get; set; }
public int CapacitorBLocation { get; set; }
public int CapacitorCLocation { get; set; }
}
对于PossibleSolution中的给定行(即第2行),我需要查看是否
OR
OR
理想情况下是一个布尔值,如果条件为真/假,则表示为true
这是我到目前为止所尝试的但是它无法正常工作
int D = 4; // The row i care about
int E = PossibleSolution[D].CapacitorALocation;
int F = PossibleSolution[D].CapacitorBLocation;
int G = PossibleSolution[D].CapacitorCLocation;
var fixedSet = new HashSet<int>() {E};
if (!FirstIteration.Any(x => fixedSet.SetEquals(new[] { x.CapacitorALocation, x.CapacitorBLocation, x.CapacitorCLocation })))
{
fixedSet = new HashSet<int>() {F};
if (!FirstIteration.Any(x => fixedSet.SetEquals(new[] { x.CapacitorALocation, x.CapacitorBLocation, x.CapacitorCLocation })))
{
fixedSet = new HashSet<int>() {G};
if (!FirstIteration.Any(x => fixedSet.SetEquals(new[] { x.CapacitorALocation, x.CapacitorBLocation, x.CapacitorCLocation })))
{
//Match does not exist so do some real work here ......
}
}
}
感谢, DAMO
答案 0 :(得分:1)
因为在所有三种情况下都会检查特定位置
在
中不存在FirstIteration.CapacitorALocation
或FirstIteration.CapacitorBLocation
或FirstIteration.CapacitorCLocation
您可以在一个Set<int>
中收集所有这些位置,以便快速检查:
var firstIterationLocations = new HashSet<int>(
firstIterationCapacitorList
.Where(loc => loc.Iteration == X)
.SelectMany(
loc => new[] {loc.CapacitorALocation, loc.CapacitorBLocation, loc.CapacitorCLocation}
)
);
掌握firstIterationLocations
后,您可以按照以下方式构建条件:
if (!(firstIterationLocations.Contains(PossibleSolution[D].CapacitorALocation)
|| firstIterationLocations.Contains(PossibleSolution[D].CapacitorBLocation)
|| firstIterationLocations.Contains(PossibleSolution[D].CapacitorCLocation))
) {
...
}
答案 1 :(得分:-1)
所以你想知道的是第一组位置中的任何项目是否在第二组位置。您可以将其视为询问两组的交集是否有任何项目:
public static bool Match(FirstIterationCapacitors first
, FirstIterationCapacitors second)
{
return new[]{
first.CapacitorALocation,
first.CapacitorBLocation,
first.CapacitorCLocation
}.Intersect(new[]{
second.CapacitorALocation,
second.CapacitorBLocation,
second.CapacitorCLocation,
})
.Any();
}