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

时间:2013-09-04 20:52:46

标签: c# linq list listview

我有一个列表

        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行),我需要查看是否

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

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

      1. PossibleSolution.CapacitorCLocation不存在于 FirstIteration.CapacitorALocation(等于迭代X)或 FirstIteration.CapacitorBLocation(等于迭代X)或 FirstIteration.CapacitorCLocation(等于迭代X)
      2. 理想情况下是一个布尔值,如果条件为真/假,则表示为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

2 个答案:

答案 0 :(得分: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))
) {
    ...
}

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