Tiago Silva - 4 years ago 77
C# Question

# Search Through List Array

I have a array of lists lists[k] (multiple lists).

I want to search if a variable PointF from a list[k] is equals to any of the elements of the lists EXCEPT the list where that variable PointF comes from.

Something like search on lists k+1,k+2,k+n and k-1,k-2,k-n except k.

``````for (int k = 0; k < drone.Length; k++)
{
for (int i = 0; i <= 10; i++)
{
for (int j = 0; j <= 10; j++)
{
}
}

// now I want to search if a variable PointF
// from a list[k] is equals to any of the elements
// of the lists except the list where that variable PointF comes from
}
``````

If you know `k` (the index of the list where your point came from) you can do this with this little linq implementation:

``````private bool PointExistsInAnotherList(List<PointF>[] lists, int k, PointF p)
{
return Enumerable.Range(0, lists.Length)
.Where(i => i != k)
.Any(i => lists[i].Any(point => point.Equals(p)));

}
``````

This checks for all lists except `lists[k]` if any of the points in that list equals the given point `p`. If one matching point is found, it returns `true` immediatly.

If you want to know the index of the list that contains the matching point, you can do this:

``````private int PointExistsInAnotherList(List<PointF>[] lists, int k, PointF p)
{
for (int i=0; i<lists.Length; i++)
{
if (i == k) continue;
if (lists[i].Any(point => point.Equals(p)) return i;
}

return -1;
}
``````

This returns the index (`i`) of the first list that contains a point equal to `p`, or `-1` if no matching point was found.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download