查看任何列表项中是否存在任何项

本文关键字:是否 存在 任何项 任何 列表 | 更新日期: 2023-09-27 18:30:23

>我有一个列表

        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. PossibleSolution.CapacitorALocation 不存在于第一次迭代.电容器(等于迭代 X)或FirstIteration.CapacitorBLocation(等于迭代X)或FirstIteration.CapacitorCLocation(等于迭代X)

  1. PossibleSolution.CapacitorBocation 不存在于第一次迭代.电容器(等于迭代 X)或FirstIteration.CapacitorBLocation(等于迭代X)或FirstIteration.CapacitorCLocation(等于迭代X)

  1. PossibleSolution.CapacitorCLocation 在 中不存在第一次迭代.电容器(等于迭代 X)或FirstIteration.CapacitorBLocation(等于迭代X)或FirstIteration.CapacitorCLocation(等于迭代X)

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

这是我到目前为止尝试过的,但它不起作用

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

谢谢达摩

查看任何列表项中是否存在任何项

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

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))
) {
    ...
}
因此,

您想知道的是第一组位置中的任何项目是否位于第二组位置中。 你可以把它想象成询问两个集合的交集是否有任何项目:

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