0

我有一个清单

        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; }      

    }

对于可能解决方案中的给定行(即第 2 行),我需要查看是否

  1. 可能Solution.CapacitorALocation 不存在于FirstIteration.CapacitorALocation(等于迭代X)或FirstIteration.CapacitorBLocation(等于迭代X)或FirstIteration.CapacitorCLocation(等于迭代X)

或者

  1. PossibleSolution.CapacitorBLocation 不存在于 FirstIteration.CapacitorALocation(等于迭代 X)或 FirstIteration.CapacitorBLocation(等于迭代 X)或 FirstIteration.CapacitorCLocation(等于迭代 X)

或者

  1. 在FirstIteration.CapacitorALocation(等于迭代X)或FirstIteration.CapacitorBLocation(等于迭代X)或FirstIteration.CapacitorCLocation(等于迭代X)中不存在PossibleSolution.CapacitorCLocation

理想情况下,如果条件为真/假,则布尔值表示真假

这是我迄今为止尝试过的,但它不起作用

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 ......

}

}

}

谢谢,达摩

4

2 回答 2

1

因为在您的所有三个条件中,您都会检查特定位置是否

不存在FirstIteration.CapacitorALocationFirstIteration.CapacitorBLocationFirstIteration.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))
) {
    ...
}
于 2013-09-04T21:06:36.357 回答
-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();
}
于 2013-09-04T21:02:15.343 回答